1 /*
2 * Copyright (c) 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 "dac_if.h"
10 #include "hdf_io_service_if.h"
11 #include "platform_core.h"
12 #include "hdf_log.h"
13 #include "osal_mem.h"
14 #include "securec.h"
15
16 #define HDF_LOG_TAG dac_if_c
17 #define DAC_SERVICE_NAME "HDF_PLATFORM_DAC_MANAGER"
18
DacManagerServiceGet(void)19 static void *DacManagerServiceGet(void)
20 {
21 static struct HdfIoService *service = NULL;
22
23 if (service != NULL) {
24 return service;
25 }
26 service = (struct HdfIoService *)HdfIoServiceBind("HDF_PLATFORM_DAC_MANAGER");
27 if (service == NULL) {
28 HDF_LOGE("%s: failed to get dac manager service!", __func__);
29 }
30 return service;
31 }
32
DacOpen(uint32_t number)33 DevHandle DacOpen(uint32_t number)
34 {
35 int32_t ret;
36 struct HdfIoService *service = NULL;
37 struct HdfSBuf *data = NULL;
38 struct HdfSBuf *reply = NULL;
39 uint32_t handle;
40
41 service = DacManagerServiceGet();
42 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
43 HDF_LOGE("%s: service is invalid", __func__);
44 return NULL;
45 }
46
47 data = HdfSbufObtainDefaultSize();
48 if (data == NULL) {
49 HDF_LOGE("DacOpen: malloc data failed!");
50 return NULL;
51 }
52 reply = HdfSbufObtainDefaultSize();
53 if (reply == NULL) {
54 HDF_LOGE("DacOpen: malloc reply failed!");
55 HdfSbufRecycle(data);
56 return NULL;
57 }
58
59 if (!HdfSbufWriteUint32(data, number)) {
60 HDF_LOGE("DacOpen: write number failed!");
61 HdfSbufRecycle(data);
62 HdfSbufRecycle(reply);
63 return NULL;
64 }
65
66 ret = service->dispatcher->Dispatch(&service->object, DAC_IO_OPEN, data, reply);
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("DacOpen: service call open failed:%d", ret);
69 HdfSbufRecycle(data);
70 HdfSbufRecycle(reply);
71 return NULL;
72 }
73
74 if (!HdfSbufReadUint32(reply, &handle)) {
75 HDF_LOGE("DacOpen: read handle failed!");
76 HdfSbufRecycle(data);
77 HdfSbufRecycle(reply);
78 return NULL;
79 }
80 HdfSbufRecycle(data);
81 HdfSbufRecycle(reply);
82 return (DevHandle)(uintptr_t)handle;
83 }
84
DacClose(DevHandle handle)85 void DacClose(DevHandle handle)
86 {
87 int32_t ret;
88 struct HdfIoService *service = NULL;
89 struct HdfSBuf *data = NULL;
90
91 if (handle == NULL) {
92 HDF_LOGE("%s: handle is invalid", __func__);
93 return;
94 }
95
96 service = (struct HdfIoService *)DacManagerServiceGet();
97 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
98 HDF_LOGE("%s: service is invalid", __func__);
99 return;
100 }
101
102 data = HdfSbufObtainDefaultSize();
103 if (data == NULL) {
104 return;
105 }
106
107 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
108 HDF_LOGE("DacClose: write handle failed!");
109 HdfSbufRecycle(data);
110 return;
111 }
112
113 ret = service->dispatcher->Dispatch(&service->object, DAC_IO_CLOSE, data, NULL);
114 if (ret != HDF_SUCCESS) {
115 HDF_LOGE("DacClose: close handle failed:%d", ret);
116 }
117 HdfSbufRecycle(data);
118 }
119
DacWrite(DevHandle handle,uint32_t channel,uint32_t val)120 int32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val)
121 {
122 int32_t ret;
123 struct HdfIoService *service = NULL;
124 struct HdfSBuf *data = NULL;
125
126 if (handle == NULL) {
127 HDF_LOGE("%s: handle is invalid", __func__);
128 return HDF_FAILURE;
129 }
130
131 service = (struct HdfIoService *)DacManagerServiceGet();
132 if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
133 HDF_LOGE("%s: service is invalid", __func__);
134 return HDF_ERR_INVALID_PARAM;
135 }
136
137 data = HdfSbufObtainDefaultSize();
138 if (data == NULL) {
139 return HDF_ERR_MALLOC_FAIL;
140 }
141
142 if (!HdfSbufWriteUint32(data, (uint32_t)(uintptr_t)handle)) {
143 HDF_LOGE("DacWrite: write handle failed!");
144 HdfSbufRecycle(data);
145 return HDF_ERR_IO;
146 }
147
148 if (!HdfSbufWriteUint32(data, channel)) {
149 HDF_LOGE("DacWrite: write channel failed!");
150 HdfSbufRecycle(data);
151 return HDF_ERR_IO;
152 }
153
154 if (!HdfSbufWriteUint32(data, val)) {
155 HDF_LOGE("DacWrite: write val failed!");
156 HdfSbufRecycle(data);
157 return HDF_ERR_IO;
158 }
159
160 ret = service->dispatcher->Dispatch(&service->object, DAC_IO_WRITE, data, NULL);
161 if (ret != HDF_SUCCESS) {
162 HDF_LOGE("DacWrite: write dac failed:%d", ret);
163 }
164 HdfSbufRecycle(data);
165 return ret;
166 }
167