1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hdf_power_manager.h"
10 #include "hdf_log.h"
11 #include "hdf_pm_reg.h"
12 #include "osal_mem.h"
13 #include "power_state_token.h"
14
15 #define HDF_LOG_TAG hdf_power_manager
16
17 struct PmTaskQueue {
18 struct HdfTaskQueue *taskQueue;
19 };
20
HdfPmTaskQueueInstance(void)21 static struct PmTaskQueue *HdfPmTaskQueueInstance(void)
22 {
23 static struct PmTaskQueue pmTaskQueue = {NULL};
24 return &pmTaskQueue;
25 }
26
HdfPmTaskQueueInit(HdfTaskFunc func)27 struct PmTaskQueue *HdfPmTaskQueueInit(HdfTaskFunc func)
28 {
29 struct PmTaskQueue *pmTaskQueue = HdfPmTaskQueueInstance();
30
31 if (pmTaskQueue->taskQueue == NULL) {
32 pmTaskQueue->taskQueue = HdfTaskQueueCreate(func, "pm_queue");
33 if (pmTaskQueue->taskQueue != NULL) {
34 HDF_LOGI("%{public}s HdfTaskQueueCreate success", __func__);
35 }
36 }
37
38 return pmTaskQueue;
39 }
40
HdfPmTaskQueueDestroy(void)41 void HdfPmTaskQueueDestroy(void)
42 {
43 struct PmTaskQueue *pmTaskQueue = HdfPmTaskQueueInstance();
44 HdfTaskQueueDestroy(pmTaskQueue->taskQueue);
45 pmTaskQueue->taskQueue = NULL;
46 }
47
PmTaskFunc(struct HdfTaskType * para)48 static int32_t PmTaskFunc(struct HdfTaskType *para)
49 {
50 struct HdfPmRequest *pmRequest = NULL;
51 struct IPowerStateToken *tokenIf = NULL;
52
53 if (para == NULL) {
54 return HDF_FAILURE;
55 }
56
57 pmRequest = CONTAINER_OF(para, struct HdfPmRequest, task);
58 tokenIf = (struct IPowerStateToken *)pmRequest->token;
59 if (pmRequest->pmType == HDF_PM_REQUEST_ACQUIRE) {
60 if ((tokenIf != NULL) && (tokenIf->AcquireWakeLock != NULL)) {
61 tokenIf->AcquireWakeLock(tokenIf);
62 }
63 } else if (pmRequest->pmType == HDF_PM_REQUEST_RELEASE) {
64 if ((tokenIf != NULL) && (tokenIf->ReleaseWakeLock != NULL)) {
65 tokenIf->ReleaseWakeLock(tokenIf);
66 }
67 }
68 OsalMemFree(pmRequest);
69 return HDF_SUCCESS;
70 }
71
HdfPmTaskPut(struct PowerStateToken * powerToken,HDF_PM_REQUEST_TYPE type)72 void HdfPmTaskPut(struct PowerStateToken *powerToken, HDF_PM_REQUEST_TYPE type)
73 {
74 struct HdfPmRequest *pmRequest = NULL;
75 struct PmTaskQueue *pmTaskQueue = NULL;
76
77 if (powerToken == NULL) {
78 return;
79 }
80
81 pmTaskQueue = HdfPmTaskQueueInstance();
82 pmRequest = (struct HdfPmRequest *)OsalMemCalloc(sizeof(*pmRequest));
83 if (pmRequest == NULL) {
84 HDF_LOGI("%{public}s OsalMemCalloc fail", __func__);
85 return;
86 }
87
88 pmRequest->token = powerToken;
89 pmRequest->pmType = type;
90 pmRequest->task.func = PmTaskFunc;
91 HdfTaskEnqueue(pmTaskQueue->taskQueue, &pmRequest->task);
92 }
93
HdfPowerManagerInit(void)94 int32_t HdfPowerManagerInit(void)
95 {
96 DevMgrPmRegister();
97 HdfPmTaskQueueInit(NULL);
98 return HDF_SUCCESS;
99 }
100
HdfPowerManagerExit(void)101 void HdfPowerManagerExit(void)
102 {
103 HdfPmTaskQueueDestroy();
104 }