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