1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "disp_hal.h"
17 #include <securec.h>
18 #include "hdf_io_service_if.h"
19 #include "hdf_log.h"
20 #include "hdf_sbuf.h"
21
22 #define OFFSET_TWO_BYTE 16
23 #define MASK_TWO_BYTE 0xffff
24
DispCmdSend(const uint32_t cmd,struct HdfSBuf * reqData,struct HdfSBuf * respData)25 static int32_t DispCmdSend(const uint32_t cmd, struct HdfSBuf *reqData, struct HdfSBuf *respData)
26 {
27 struct HdfIoService *dispService = NULL;
28
29 dispService = HdfIoServiceBind(DISP_SERVICE_NAME);
30 if ((dispService == NULL) || (dispService->dispatcher == NULL) || (dispService->dispatcher->Dispatch == NULL)) {
31 HDF_LOGE("%s:bad remote service found", __func__);
32 goto EXIT;
33 }
34 int32_t ret = dispService->dispatcher->Dispatch(&dispService->object, cmd, reqData, respData);
35 if (ret != DISPLAY_SUCCESS) {
36 HDF_LOGE("%s: cmd=%u, ret=%d", __func__, cmd, ret);
37 goto EXIT;
38 }
39 HDF_LOGI("%s: cmd=%u, ret=%d", __func__, cmd, ret);
40 HdfIoServiceRecycle(dispService);
41 return DISPLAY_SUCCESS;
42
43 EXIT:
44 HdfIoServiceRecycle(dispService);
45 return DISPLAY_FAILURE;
46 }
47
GetInfo(uint32_t devId,struct DispInfo * info)48 static int32_t GetInfo(uint32_t devId, struct DispInfo *info)
49 {
50 struct DispInfo *tmpInfo = NULL;
51 struct HdfSBuf *data = NULL;
52 struct HdfSBuf *reply = NULL;
53
54 if (info == NULL) {
55 HDF_LOGE("%s: invalid param", __func__);
56 return DISPLAY_FAILURE;
57 }
58
59 data = HdfSbufObtainDefaultSize();
60 if (data == NULL) {
61 HDF_LOGE("%s: obtain data sbuf fail", __func__);
62 return DISPLAY_FAILURE;
63 }
64 reply = HdfSbufObtainDefaultSize();
65 if (reply == NULL) {
66 HDF_LOGE("%s: obtain reply sbuf fail", __func__);
67 goto EXIT;
68 }
69 if (!HdfSbufWriteUint32(data, devId)) {
70 HDF_LOGE("HdfSbufWriteUint32 failure");
71 goto EXIT;
72 }
73 if (DispCmdSend(DISP_CMD_GET_PANELINFO, data, reply) != DISPLAY_SUCCESS) {
74 HDF_LOGE("cmd:DISP_CMD_GET_PANEL_INFO failure");
75 goto EXIT;
76 }
77 uint32_t dataSize = 0;
78 if (!HdfSbufReadBuffer(reply, (const void **)(&tmpInfo), &dataSize) || dataSize != sizeof(struct DispInfo)) {
79 HDF_LOGE("HdfSbufReadBuffer failure");
80 goto EXIT;
81 }
82 if (memcpy_s(info, sizeof(struct DispInfo), tmpInfo, dataSize) != EOK) {
83 HDF_LOGE("memcpy_s failure");
84 goto EXIT;
85 }
86 HDF_LOGI("tmpInfo->width = %u, tmpInfo->height = %u", tmpInfo->width, tmpInfo->height);
87 HDF_LOGI("tmpInfo->hbp = %u, tmpInfo->hfp = %u", tmpInfo->hbp, tmpInfo->hfp);
88 HDF_LOGI("tmpInfo->frameRate = %u", tmpInfo->frameRate);
89 HDF_LOGI("tmpInfo->intfSync = %d", tmpInfo->intfSync);
90 HdfSbufRecycle(data);
91 HdfSbufRecycle(reply);
92 return DISPLAY_SUCCESS;
93
94 EXIT:
95 HdfSbufRecycle(data);
96 HdfSbufRecycle(reply);
97 return DISPLAY_FAILURE;
98 }
99
DispGetParaProcess(uint32_t devId,const uint32_t cmd,uint32_t * value)100 static int32_t DispGetParaProcess(uint32_t devId, const uint32_t cmd, uint32_t *value)
101 {
102 int32_t ret;
103 struct HdfSBuf *data = NULL;
104 struct HdfSBuf *reply = NULL;
105
106 if (value == NULL) {
107 HDF_LOGE("%s: invalid param", __func__);
108 return DISPLAY_FAILURE;
109 }
110
111 data = HdfSbufObtainDefaultSize();
112 if (data == NULL) {
113 HDF_LOGE("%s: obtain data sbuf fail", __func__);
114 return DISPLAY_FAILURE;
115 }
116 reply = HdfSbufObtainDefaultSize();
117 if (reply == NULL) {
118 HDF_LOGE("%s: obtain reply sbuf fail", __func__);
119 goto EXIT;
120 }
121 if (!HdfSbufWriteUint32(data, devId)) {
122 HDF_LOGE("HdfSbufWriteUint32 failure");
123 goto EXIT;
124 }
125 ret = DispCmdSend(cmd, data, reply);
126 if (ret != DISPLAY_SUCCESS) {
127 HDF_LOGE("cmd:DISP_CMD_GET_PANEL_INFO failure");
128 goto EXIT;
129 }
130 if (!HdfSbufReadUint32(reply, value)) {
131 HDF_LOGE("HdfSbufReadUint32 failure");
132 goto EXIT;
133 }
134 HdfSbufRecycle(data);
135 HdfSbufRecycle(reply);
136 return DISPLAY_SUCCESS;
137
138 EXIT:
139 HdfSbufRecycle(data);
140 HdfSbufRecycle(reply);
141 return DISPLAY_FAILURE;
142 }
143
DispEventProcess(uint32_t devId,const uint32_t cmd,uint32_t val)144 static int32_t DispEventProcess(uint32_t devId, const uint32_t cmd, uint32_t val)
145 {
146 int32_t ret;
147
148 struct HdfSBuf *data = HdfSbufObtainDefaultSize();
149 if (data == NULL) {
150 HDF_LOGE("%s: obtain data sbuf fail", __func__);
151 return DISPLAY_FAILURE;
152 }
153 uint32_t para = (devId << OFFSET_TWO_BYTE) | (val & 0xffff);
154 if (!HdfSbufWriteUint32(data, para)) {
155 HDF_LOGE("HdfSbufWriteUint32 failure\n");
156 goto EXIT;
157 }
158 ret = DispCmdSend(cmd, data, NULL);
159 if (ret != DISPLAY_SUCCESS) {
160 HDF_LOGE("cmd:DISP_CMD_SET_%s failure\n", (cmd == DISP_CMD_SET_POWERSTATUS) ? "POWERMODE" : "BACKLIGHT");
161 goto EXIT;
162 }
163 HdfSbufRecycle(data);
164 return DISPLAY_SUCCESS;
165
166 EXIT:
167 HdfSbufRecycle(data);
168 return DISPLAY_FAILURE;
169 }
170
SetPowerStatus(uint32_t devId,DispPowerStatus status)171 static int32_t SetPowerStatus(uint32_t devId, DispPowerStatus status)
172 {
173 return DispEventProcess(devId, DISP_CMD_SET_POWERSTATUS, status);
174 }
175
GetPowerStatus(uint32_t devId,DispPowerStatus * pStatus)176 static int32_t GetPowerStatus(uint32_t devId, DispPowerStatus *pStatus)
177 {
178 return DispGetParaProcess(devId, DISP_CMD_GET_POWERSTATUS, pStatus);
179 }
180
SetBacklight(uint32_t devId,uint32_t level)181 static int32_t SetBacklight(uint32_t devId, uint32_t level)
182 {
183 return DispEventProcess(devId, DISP_CMD_SET_BACKLIGHT, level);
184 }
185
GetBacklight(uint32_t devId,uint32_t * level)186 static int32_t GetBacklight(uint32_t devId, uint32_t *level)
187 {
188 return DispGetParaProcess(devId, DISP_CMD_GET_BACKLIGHT, level);
189 }
190
GetHalFuncs(void)191 HalFuncs *GetHalFuncs(void)
192 {
193 static HalFuncs *hFuncs = NULL;
194
195 if (hFuncs == NULL) {
196 hFuncs = (HalFuncs *)malloc(sizeof(HalFuncs));
197 if (hFuncs == NULL) {
198 HDF_LOGE("%s: malloc fail", __func__);
199 return NULL;
200 }
201 (void)memset_s(hFuncs, sizeof(HalFuncs), 0, sizeof(HalFuncs));
202 hFuncs->SetPowerStatus = SetPowerStatus;
203 hFuncs->GetPowerStatus = GetPowerStatus;
204 hFuncs->SetBacklight = SetBacklight;
205 hFuncs->GetBacklight = GetBacklight;
206 hFuncs->GetInfo = GetInfo;
207 }
208 return hFuncs;
209 }
210