更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
power_proc.c
浏览该文件的文档.
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// /proc 下的电源适配层
39//写电源锁信息
40static int PowerLockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
41{
42 (void)pf;
43 (void)count;
44 (void)ppos;
45 return -LOS_PmLockRequest(buf);
46}
47///读取电源锁信息
48static int PowerLockRead(struct SeqBuf *m, void *v)
49{
50 (void)v;
51
53 return 0;
54}
55///proc 拿电源锁操作
56static const struct ProcFileOperations PowerLock = {
58 .read = PowerLockRead,
59};
60//proc 释放锁操作
61static int PowerUnlockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
62{
63 (void)pf;
64 (void)count;
65 (void)ppos;
66 return -LOS_PmLockRelease(buf);
67}
68
69static const struct ProcFileOperations PowerUnlock = {
71 .read = PowerLockRead,
72};
73
74static int PowerModeWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
75{
76 (void)pf;
77 (void)count;
78 (void)ppos;
79
81
82 if (buf == NULL) {
83 return 0;
84 }
85
86 if (strcmp(buf, "normal") == 0) {
88 } else if (strcmp(buf, "light") == 0) {
90 } else if (strcmp(buf, "deep") == 0) {
91 mode = LOS_SYS_DEEP_SLEEP;
92 } else if (strcmp(buf, "shutdown") == 0) {
93 mode = LOS_SYS_SHUTDOWN;
94 } else {
95 PRINT_ERR("Unsupported hibernation mode: %s\n", buf);
96 return -EINVAL;
97 }
98
99 return -LOS_PmModeSet(mode);
100}
101
102static int PowerModeRead(struct SeqBuf *m, void *v)
103{
104 (void)v;
105
106 LosBufPrintf(m, "normal light deep shutdown\n");
107 return 0;
108}
109
110static const struct ProcFileOperations PowerMode = {
112 .read = PowerModeRead,
113};
114
115static int PowerCountRead(struct SeqBuf *m, void *v)
116{
117 (void)v;
118 UINT32 count = LOS_PmReadLock();
119
120 LosBufPrintf(m, "%u\n", count);
121 return 0;
122}
123
124static int PowerCountWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
125{
126 (void)pf;
127 (void)count;
128 (void)ppos;
129
130 int weakCount;
131
132 if (buf == NULL) {
133 return 0;
134 }
135
136 weakCount = atoi(buf);
137 return -LOS_PmSuspend(weakCount);
138}
139
140static const struct ProcFileOperations PowerCount = {
142 .read = PowerCountRead,
143};
144
145#define POWER_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
146#define OS_POWER_PRIVILEGE 7 //电源特权
147// /proc/power 的初始化
148void ProcPmInit(void)
149{
150 struct ProcDirEntry *power = CreateProcEntry("power", S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH, NULL);
151 if (power == NULL) {//创建 文件夹 /proc/power
152 PRINT_ERR("create /proc/power error!\n");
153 return;
154 }
155 power->uid = OS_POWER_PRIVILEGE;
156 power->gid = OS_POWER_PRIVILEGE;
157
158 struct ProcDirEntry *mode = CreateProcEntry("power/power_mode", POWER_FILE_MODE, NULL);
159 if (mode == NULL) {//创建 文件夹 /proc/power/power_mode
160 PRINT_ERR("create /proc/power/power_mode error!\n");
161 goto FREE_POWER;
162 }
163 mode->procFileOps = &PowerMode;
164 mode->uid = OS_POWER_PRIVILEGE;
165 mode->gid = OS_POWER_PRIVILEGE;
166
167 struct ProcDirEntry *lock = CreateProcEntry("power/power_lock", POWER_FILE_MODE, NULL);
168 if (lock == NULL) {//创建 文件夹 /proc/power/power_lock
169 PRINT_ERR("create /proc/power/power_lock error!\n");
170 goto FREE_MODE;
171 }
172 lock->procFileOps = &PowerLock;
173 lock->uid = OS_POWER_PRIVILEGE;
174 lock->gid = OS_POWER_PRIVILEGE;
175
176 struct ProcDirEntry *unlock = CreateProcEntry("power/power_unlock", POWER_FILE_MODE, NULL);
177 if (unlock == NULL) {//创建 文件夹 /proc/power/power_unlock
178 PRINT_ERR("create /proc/power/power_unlock error!\n");
179 goto FREE_LOCK;
180 }
181 unlock->procFileOps = &PowerUnlock;
182 unlock->uid = OS_POWER_PRIVILEGE;
183 unlock->gid = OS_POWER_PRIVILEGE;
184
185 struct ProcDirEntry *count = CreateProcEntry("power/power_count", S_IRUSR | S_IRGRP | S_IROTH, NULL);
186 if (count == NULL) {//创建 文件夹 /proc/power/power_count
187 PRINT_ERR("create /proc/power/power_count error!\n");
188 goto FREE_UNLOCK;
189 }
190 count->procFileOps = &PowerCount;
191 count->uid = OS_POWER_PRIVILEGE;
192 count->gid = OS_POWER_PRIVILEGE;
193
194 return;
195
196FREE_UNLOCK:
197 ProcFreeEntry(unlock);
198FREE_LOCK:
199 ProcFreeEntry(lock);
200FREE_MODE:
202FREE_POWER:
203 ProcFreeEntry(power);
204 return;
205}
206#endif
void ProcFreeEntry(struct ProcDirEntry *pde)
释放
Definition: proc_file.c:388
UINT32 LOS_PmModeSet(LOS_SysSleepEnum mode)
Set low power mode.
Definition: los_pm.c:411
VOID LOS_PmLockInfoShow(struct SeqBuf *m)
显示所有电源锁信息
Definition: los_pm.c:455
UINT32 LOS_PmReadLock(VOID)
Gets the current PM lock status.
Definition: los_pm.c:664
UINT32 LOS_PmLockRequest(const CHAR *name)
Request to obtain the lock in current mode, so that the system will not enter this mode when it enter...
Definition: los_pm.c:543
UINT32 LOS_PmSuspend(UINT32 wakeCount)
The system enters the low-power flow.
Definition: los_pm.c:674
UINT32 LOS_PmLockRelease(const CHAR *name)
Release the lock in current mode so that the next time the system enters the idle task,...
Definition: los_pm.c:552
LOS_SysSleepEnum
Definition: los_pm.h:41
@ LOS_SYS_DEEP_SLEEP
Definition: los_pm.h:44
@ LOS_SYS_LIGHT_SLEEP
Definition: los_pm.h:43
@ LOS_SYS_SHUTDOWN
Definition: los_pm.h:45
@ LOS_SYS_NORMAL_SLEEP
Definition: los_pm.h:42
int LosBufPrintf(struct SeqBuf *seqBuf, const char *fmt,...)
支持可变参数 写 buf
Definition: los_seq_buf.c:133
unsigned int UINT32
Definition: los_typedef.h:57
static int PowerCountWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: power_proc.c:124
static int PowerCountRead(struct SeqBuf *m, void *v)
Definition: power_proc.c:115
static const struct ProcFileOperations PowerLock
proc 拿电源锁操作
Definition: power_proc.c:56
static int PowerLockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: power_proc.c:40
static int PowerModeRead(struct SeqBuf *m, void *v)
Definition: power_proc.c:102
static int PowerModeWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: power_proc.c:74
void ProcPmInit(void)
Definition: power_proc.c:148
static int PowerLockRead(struct SeqBuf *m, void *v)
读取电源锁信息
Definition: power_proc.c:48
static const struct ProcFileOperations PowerUnlock
Definition: power_proc.c:69
static const struct ProcFileOperations PowerMode
Definition: power_proc.c:110
static int PowerUnlockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: power_proc.c:61
static const struct ProcFileOperations PowerCount
Definition: power_proc.c:140
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
create a proc node
Definition: proc_file.c:364
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
uint uid
Definition: proc_fs.h:102
atomic_t count
Definition: proc_fs.h:110
const struct ProcFileOperations * procFileOps
驱动程序,每个 /proc 下目录的驱动程序都不一样
Definition: proc_fs.h:106
mode_t mode
模式(读|写...)
Definition: proc_fs.h:104
uint gid
Definition: proc_fs.h:103
Proc文件结构体,对标 FILE 结构体
Definition: proc_fs.h:121
真正最后能操作pro file的接口,proc本质是个内存文件系统, vfs - > ProcFileOperations
Definition: proc_fs.h:89
ssize_t(* write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: proc_fs.h:91
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void