1 /*
2 * Copyright (c) 2021-2023 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/dac_core.h"
10 #include "asm/platform.h"
11 #include "device_resource_if.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14 #include "los_hwi.h"
15 #include "osal_io.h"
16 #include "osal_mem.h"
17 #include "osal_time.h"
18
19 #define HDF_LOG_TAG dac_virtual
20
21 struct VirtualDacDevice {
22 struct DacDevice device;
23 uint32_t deviceNum;
24 uint32_t validChannel;
25 uint32_t rate;
26 };
27
VirtualDacWrite(struct DacDevice * device,uint32_t channel,uint32_t val)28 static int32_t VirtualDacWrite(struct DacDevice *device, uint32_t channel, uint32_t val)
29 {
30 (void)device;
31 (void)channel;
32 (void)val;
33 HDF_LOGI("VirtualDacWrite: done!");
34
35 return HDF_SUCCESS;
36 }
37
VirtualDacStart(struct DacDevice * device)38 static inline int32_t VirtualDacStart(struct DacDevice *device)
39 {
40 (void)device;
41 HDF_LOGI("VirtualDacStart: done!");
42 return HDF_SUCCESS;
43 }
44
VirtualDacStop(struct DacDevice * device)45 static inline int32_t VirtualDacStop(struct DacDevice *device)
46 {
47 (void)device;
48 HDF_LOGI("VirtualDacStop: done!");
49 return HDF_SUCCESS;
50 }
51
VirtualDacDeviceInit(struct VirtualDacDevice * virtual)52 static inline void VirtualDacDeviceInit(struct VirtualDacDevice *virtual)
53 {
54 HDF_LOGI("VirtualDacDeviceInit: device:%u init done!", virtual->deviceNum);
55 }
56
57 static const struct DacMethod g_method = {
58 .write = VirtualDacWrite,
59 .stop = VirtualDacStop,
60 .start = VirtualDacStart,
61 };
62
VirtualDacReadDrs(struct VirtualDacDevice * virtual,const struct DeviceResourceNode * node)63 static int32_t VirtualDacReadDrs(struct VirtualDacDevice *virtual, const struct DeviceResourceNode *node)
64 {
65 struct DeviceResourceIface *drsOps = NULL;
66
67 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
68 if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetUint16 == NULL) {
69 HDF_LOGE("VirtualDacReadDrs: invalid drs ops fail!");
70 return HDF_FAILURE;
71 }
72 if (drsOps->GetUint32(node, "deviceNum", &virtual->deviceNum, 0) != HDF_SUCCESS) {
73 HDF_LOGE("VirtualDacReadDrs: read deviceNum fail!");
74 return HDF_ERR_IO;
75 }
76 if (drsOps->GetUint32(node, "validChannel", &virtual->validChannel, 0) != HDF_SUCCESS) {
77 HDF_LOGE("VirtualDacReadDrs: read validChannel fail!");
78 return HDF_ERR_IO;
79 }
80 if (drsOps->GetUint32(node, "rate", &virtual->rate, 0) != HDF_SUCCESS) {
81 HDF_LOGE("VirtualDacReadDrs: read rate fail!");
82 return HDF_ERR_IO;
83 }
84 return HDF_SUCCESS;
85 }
86
VirtualDacParseAndInit(struct HdfDeviceObject * device,const struct DeviceResourceNode * node)87 static int32_t VirtualDacParseAndInit(struct HdfDeviceObject *device, const struct DeviceResourceNode *node)
88 {
89 int32_t ret;
90 struct VirtualDacDevice *virtual = NULL;
91 (void)device;
92
93 virtual = (struct VirtualDacDevice *)OsalMemCalloc(sizeof(*virtual));
94 if (virtual == NULL) {
95 HDF_LOGE("VirtualDacParseAndInit: malloc virtual fail!");
96 return HDF_ERR_MALLOC_FAIL;
97 }
98
99 ret = VirtualDacReadDrs(virtual, node);
100 if (ret != HDF_SUCCESS) {
101 HDF_LOGE("VirtualDacParseAndInit: read drs fail, ret: %d!", ret);
102 OsalMemFree(virtual);
103 virtual = NULL;
104 return ret;
105 }
106
107 VirtualDacDeviceInit(virtual);
108 virtual->device.priv = (void *)node;
109 virtual->device.devNum = virtual->deviceNum;
110 virtual->device.ops = &g_method;
111 ret = DacDeviceAdd(&virtual->device);
112 if (ret != HDF_SUCCESS) {
113 HDF_LOGE("VirtualDacParseAndInit: add dac device fail, ret = %d!", ret);
114 OsalMemFree(virtual);
115 virtual = NULL;
116 return ret;
117 }
118
119 return HDF_SUCCESS;
120 }
121
VirtualDacInit(struct HdfDeviceObject * device)122 static int32_t VirtualDacInit(struct HdfDeviceObject *device)
123 {
124 int32_t ret;
125 const struct DeviceResourceNode *childNode = NULL;
126
127 if (device == NULL || device->property == NULL) {
128 HDF_LOGE("VirtualDacInit: device or property is null!");
129 return HDF_ERR_INVALID_OBJECT;
130 }
131
132 ret = HDF_SUCCESS;
133 DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) {
134 ret = VirtualDacParseAndInit(device, childNode);
135 if (ret != HDF_SUCCESS) {
136 break;
137 }
138 }
139 return ret;
140 }
141
VirtualDacRemoveByNode(const struct DeviceResourceNode * node)142 static void VirtualDacRemoveByNode(const struct DeviceResourceNode *node)
143 {
144 int32_t ret;
145 int16_t devNum;
146 struct DacDevice *device = NULL;
147 struct VirtualDacDevice *virtual = NULL;
148 struct DeviceResourceIface *drsOps = NULL;
149
150 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
151 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
152 HDF_LOGE("VirtualDacRemoveByNode: invalid drs ops fail!");
153 return;
154 }
155
156 ret = drsOps->GetUint16(node, "devNum", (uint16_t *)&devNum, 0);
157 if (ret != HDF_SUCCESS) {
158 HDF_LOGE("VirtualDacRemoveByNode: read devNum fail, ret: %d!", ret);
159 return;
160 }
161
162 device = DacDeviceGet(devNum);
163 if (device != NULL && device->priv == node) {
164 DacDevicePut(device);
165 DacDeviceRemove(device);
166 virtual = (struct VirtualDacDevice *)device;
167 OsalMemFree(virtual);
168 }
169 return;
170 }
171
VirtualDacRelease(struct HdfDeviceObject * device)172 static void VirtualDacRelease(struct HdfDeviceObject *device)
173 {
174 const struct DeviceResourceNode *childNode = NULL;
175
176 if (device == NULL || device->property == NULL) {
177 HDF_LOGE("VirtualDacRelease: device or property is null!");
178 return;
179 }
180
181 DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) {
182 VirtualDacRemoveByNode(childNode);
183 }
184 }
185
186 struct HdfDriverEntry g_dacDriverEntry = {
187 .moduleVersion = 1,
188 .Init = VirtualDacInit,
189 .Release = VirtualDacRelease,
190 .moduleName = "virtual_dac_driver",
191 };
192 HDF_INIT(g_dacDriverEntry);
193