1 /*
2 * Copyright (c) 2021 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 "adc_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 adc_test_driver_c
16
17 static struct AdcTestConfig g_config;
18
AdcTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t AdcTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21 HDF_LOGD("%s: enter!", __func__);
22
23 if (cmd == 0) {
24 if (reply == NULL) {
25 HDF_LOGE("%s: reply is null!", __func__);
26 return HDF_ERR_INVALID_PARAM;
27 }
28 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
29 HDF_LOGE("%s: write reply failed", __func__);
30 return HDF_ERR_IO;
31 }
32 } else {
33 return HDF_ERR_NOT_SUPPORT;
34 }
35
36 return HDF_SUCCESS;
37 }
38
AdcTestReadConfig(struct AdcTestConfig * config,const struct DeviceResourceNode * node)39 static int32_t AdcTestReadConfig(struct AdcTestConfig *config, const struct DeviceResourceNode *node)
40 {
41 int32_t ret;
42 struct DeviceResourceIface *drsOps = NULL;
43
44 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
45 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
46 HDF_LOGE("%s: invalid drs ops", __func__);
47 return HDF_FAILURE;
48 }
49
50 ret = drsOps->GetUint32(node, "devNum", &config->devNum, 0);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("%s: read devNum failed", __func__);
53 return ret;
54 }
55
56 ret = drsOps->GetUint32(node, "channel", &config->channel, 0);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%s: read channel failed", __func__);
59 return ret;
60 }
61
62 ret = drsOps->GetUint32(node, "maxChannel", &config->maxChannel, 0);
63 if (ret != HDF_SUCCESS) {
64 HDF_LOGE("%s: read maxChannel failed", __func__);
65 return ret;
66 }
67
68 ret = drsOps->GetUint32(node, "dataWidth", &config->dataWidth, 0);
69 if (ret != HDF_SUCCESS) {
70 HDF_LOGE("%s: read dataWidth failed", __func__);
71 return ret;
72 }
73
74 ret = drsOps->GetUint32(node, "rate", &config->rate, 0);
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%s: read rate failed", __func__);
77 return ret;
78 }
79
80 return HDF_SUCCESS;
81 }
82
AdcTestBind(struct HdfDeviceObject * device)83 static int32_t AdcTestBind(struct HdfDeviceObject *device)
84 {
85 int32_t ret;
86 static struct IDeviceIoService service;
87
88 if (device == NULL || device->property == NULL) {
89 HDF_LOGE("%s: device or config is null!", __func__);
90 return HDF_ERR_IO;
91 }
92
93 ret = AdcTestReadConfig(&g_config, device->property);
94 if (ret != HDF_SUCCESS) {
95 HDF_LOGE("%s: read config failed", __func__);
96 return ret;
97 }
98 service.Dispatch = AdcTestDispatch;
99 device->service = &service;
100 HDF_LOGI("%s: Done!", __func__);
101 return HDF_SUCCESS;
102 }
103
AdcTestInit(struct HdfDeviceObject * device)104 static int32_t AdcTestInit(struct HdfDeviceObject *device)
105 {
106 (void)device;
107 HDF_LOGI("%s: Done!", __func__);
108 return HDF_SUCCESS;
109 }
110
AdcTestRelease(struct HdfDeviceObject * device)111 static void AdcTestRelease(struct HdfDeviceObject *device)
112 {
113 if (device != NULL) {
114 device->service = NULL;
115 }
116 HDF_LOGI("%s: Done!", __func__);
117 return;
118 }
119
120 struct HdfDriverEntry g_adcTestEntry = {
121 .moduleVersion = 1,
122 .Bind = AdcTestBind,
123 .Init = AdcTestInit,
124 .Release = AdcTestRelease,
125 .moduleName = "PLATFORM_ADC_TEST",
126 };
127 HDF_INIT(g_adcTestEntry);
128