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 "gpio_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 #include "securec.h"
15
16 static struct GpioTestConfig g_config;
17
GpioTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)18 static int32_t GpioTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
19 {
20 HDF_LOGD("GpioTestDispatch: enter!");
21
22 (void)client;
23 (void)data;
24 if (cmd == 0) {
25 if (reply == NULL) {
26 HDF_LOGE("GpioTestDispatch: reply is null!");
27 return HDF_ERR_INVALID_PARAM;
28 }
29 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
30 HDF_LOGE("GpioTestDispatch: fail to write reply!");
31 return HDF_ERR_IO;
32 }
33 } else {
34 HDF_LOGE("GpioTestDispatch: cmd %d is not support!", cmd);
35 return HDF_ERR_NOT_SUPPORT;
36 }
37
38 return HDF_SUCCESS;
39 }
40
GpioReadNameTestInfos(struct GpioTestConfig * config,const struct DeviceResourceNode * node,struct DeviceResourceIface * drsOps)41 static int32_t GpioReadNameTestInfos(struct GpioTestConfig *config, const struct DeviceResourceNode *node,
42 struct DeviceResourceIface *drsOps)
43 {
44 int32_t ret;
45 const char *tempName = NULL;
46
47 ret = drsOps->GetString(node, "testNameOne", &tempName, "NULL");
48 if (ret != HDF_SUCCESS) {
49 HDF_LOGE("GpioReadNameTestInfos: fail to read testNameOne!");
50 return ret;
51 }
52
53 if (strcpy_s(config->testNameOne, NAME_SIZE_MAX, tempName) != EOK) {
54 HDF_LOGE("GpioReadNameTestInfos: fail to copy testNameOne!");
55 return HDF_FAILURE;
56 }
57
58 ret = drsOps->GetString(node, "testNameTwo", &tempName, "NULL");
59 if (ret != HDF_SUCCESS) {
60 HDF_LOGE("GpioReadNameTestInfos: fail to read testNameTwo!");
61 return ret;
62 }
63
64 if (strcpy_s(config->testNameTwo, NAME_SIZE_MAX, tempName) != EOK) {
65 HDF_LOGE("GpioReadNameTestInfos: fail to copy testNameTwo!");
66 return HDF_FAILURE;
67 }
68
69 return HDF_SUCCESS;
70 }
71
GpioTestReadConfig(struct GpioTestConfig * config,const struct DeviceResourceNode * node)72 static int32_t GpioTestReadConfig(struct GpioTestConfig *config, const struct DeviceResourceNode *node)
73 {
74 int32_t ret;
75 uint16_t tmp;
76 struct DeviceResourceIface *drsOps = NULL;
77
78 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
79 if (drsOps == NULL || drsOps->GetUint16 == NULL) {
80 HDF_LOGE("GpioTestReadConfig: invalid drs ops!");
81 return HDF_FAILURE;
82 }
83
84 ret = drsOps->GetUint16(node, "gpio", &tmp, 0);
85 if (ret != HDF_SUCCESS) {
86 HDF_LOGE("GpioTestReadConfig: fail to read gpio!");
87 return ret;
88 }
89 config->gpio = (uint16_t)tmp;
90
91 ret = drsOps->GetUint16(node, "gpioTestTwo", &tmp, 0);
92 if (ret != HDF_SUCCESS) {
93 HDF_LOGE("GpioTestReadConfig: fail to read gpioTestTwo!");
94 return ret;
95 }
96 config->gpioTestTwo = (uint16_t)tmp;
97
98 ret = drsOps->GetUint16(node, "gpioIrq", &tmp, 0);
99 if (ret != HDF_SUCCESS) {
100 HDF_LOGE("GpioTestReadConfig: fail to read gpioIrq!");
101 return ret;
102 }
103 config->gpioIrq = (uint16_t)tmp;
104
105 ret = drsOps->GetUint16(node, "testUserApi", &tmp, 0);
106 if (ret != HDF_SUCCESS) {
107 HDF_LOGW("GpioTestReadConfig: fail to read gpioIrq, using 0 as default!");
108 config->testUserApi = 0;
109 }
110 config->testUserApi = (uint16_t)tmp;
111
112 ret = GpioReadNameTestInfos(config, node, drsOps);
113 if (ret != HDF_SUCCESS) {
114 HDF_LOGE("GpioTestReadConfig: fail to read name test infos!");
115 return ret;
116 }
117
118 return HDF_SUCCESS;
119 }
120
GpioTestBind(struct HdfDeviceObject * device)121 static int32_t GpioTestBind(struct HdfDeviceObject *device)
122 {
123 int32_t ret;
124 static struct IDeviceIoService service;
125
126 if (device == NULL || device->property == NULL) {
127 HDF_LOGE("GpioTestBind: device or config is null!");
128 return HDF_ERR_IO;
129 }
130
131 ret = GpioTestReadConfig(&g_config, device->property);
132 if (ret != HDF_SUCCESS) {
133 HDF_LOGE("GpioTestBind: fail to read config!");
134 return ret;
135 }
136
137 service.Dispatch = GpioTestDispatch;
138 device->service = &service;
139 HDF_LOGI("GpioTestBind: done!");
140 return HDF_SUCCESS;
141 }
142
GpioTestInit(struct HdfDeviceObject * device)143 static int32_t GpioTestInit(struct HdfDeviceObject *device)
144 {
145 (void)device;
146 return HDF_SUCCESS;
147 }
148
GpioTestRelease(struct HdfDeviceObject * device)149 static void GpioTestRelease(struct HdfDeviceObject *device)
150 {
151 if (device != NULL) {
152 device->service = NULL;
153 }
154 return;
155 }
156
157 struct HdfDriverEntry g_gpioTestEntry = {
158 .moduleVersion = 1,
159 .Bind = GpioTestBind,
160 .Init = GpioTestInit,
161 .Release = GpioTestRelease,
162 .moduleName = "PLATFORM_GPIO_TEST",
163 };
164 HDF_INIT(g_gpioTestEntry);
165