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_test.h"
10 #include "device_resource_if.h"
11 #include "hdf_base.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14
15 #define HDF_LOG_TAG dac_test_driver_c
16
17 static struct DacTestConfig g_config;
18
DacTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t DacTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21 HDF_LOGD("DacTestDispatch: enter!");
22
23 (void)client;
24 (void)data;
25 if (cmd == 0) {
26 if (reply == NULL) {
27 HDF_LOGE("DacTestDispatch: reply is null!");
28 return HDF_ERR_INVALID_PARAM;
29 }
30 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
31 HDF_LOGE("DacTestDispatch: write reply fail!");
32 return HDF_ERR_IO;
33 }
34 } else {
35 HDF_LOGE("DacTestDispatch: cmd %d is not support!", cmd);
36 return HDF_ERR_NOT_SUPPORT;
37 }
38
39 return HDF_SUCCESS;
40 }
41
DacTestReadConfig(struct DacTestConfig * config,const struct DeviceResourceNode * node)42 static int32_t DacTestReadConfig(struct DacTestConfig *config, const struct DeviceResourceNode *node)
43 {
44 int32_t ret;
45 struct DeviceResourceIface *drsOps = NULL;
46
47 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
48 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
49 HDF_LOGE("DacTestReadConfig: invalid drs ops!");
50 return HDF_FAILURE;
51 }
52
53 ret = drsOps->GetUint32(node, "devNum", &config->devNum, 0);
54 if (ret != HDF_SUCCESS) {
55 HDF_LOGE("DacTestReadConfig: read devNum fail!");
56 return ret;
57 }
58
59 ret = drsOps->GetUint32(node, "channel", &config->channel, 0);
60 if (ret != HDF_SUCCESS) {
61 HDF_LOGE("DacTestReadConfig: read channel fail!");
62 return ret;
63 }
64
65 ret = drsOps->GetUint32(node, "maxChannel", &config->maxChannel, 0);
66 if (ret != HDF_SUCCESS) {
67 HDF_LOGE("DacTestReadConfig: read maxChannel fail!");
68 return ret;
69 }
70
71 ret = drsOps->GetUint32(node, "dataWidth", &config->dataWidth, 0);
72 if (ret != HDF_SUCCESS) {
73 HDF_LOGE("DacTestReadConfig: read dataWidth fail!");
74 return ret;
75 }
76
77 ret = drsOps->GetUint32(node, "rate", &config->rate, 0);
78 if (ret != HDF_SUCCESS) {
79 HDF_LOGE("DacTestReadConfig: read rate fail!");
80 return ret;
81 }
82
83 return HDF_SUCCESS;
84 }
85
DacTestBind(struct HdfDeviceObject * device)86 static int32_t DacTestBind(struct HdfDeviceObject *device)
87 {
88 int32_t ret;
89 static struct IDeviceIoService service;
90
91 if (device == NULL || device->property == NULL) {
92 HDF_LOGE("DacTestBind: device or config is null!");
93 return HDF_ERR_IO;
94 }
95
96 ret = DacTestReadConfig(&g_config, device->property);
97 if (ret != HDF_SUCCESS) {
98 HDF_LOGE("DacTestBind: read config fail!");
99 return ret;
100 }
101 service.Dispatch = DacTestDispatch;
102 device->service = &service;
103 HDF_LOGI("DacTestBind: done!");
104 return HDF_SUCCESS;
105 }
106
DacTestInit(struct HdfDeviceObject * device)107 static int32_t DacTestInit(struct HdfDeviceObject *device)
108 {
109 (void)device;
110 HDF_LOGI("DacTestInit: done!");
111 return HDF_SUCCESS;
112 }
113
DacTestRelease(struct HdfDeviceObject * device)114 static void DacTestRelease(struct HdfDeviceObject *device)
115 {
116 if (device != NULL) {
117 device->service = NULL;
118 }
119 HDF_LOGI("DacTestRelease: done!");
120 return;
121 }
122
123 struct HdfDriverEntry g_dacTestEntry = {
124 .moduleVersion = 1,
125 .Bind = DacTestBind,
126 .Init = DacTestInit,
127 .Release = DacTestRelease,
128 .moduleName = "PLATFORM_DAC_TEST",
129 };
130 HDF_INIT(g_dacTestEntry);
131