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 "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
15 static struct GpioTestConfig g_config;
16
GpioTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)17 static int32_t GpioTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
18 {
19 HDF_LOGD("%s: enter!", __func__);
20
21 if (cmd == 0) {
22 if (reply == NULL) {
23 HDF_LOGE("%s: reply is null!", __func__);
24 return HDF_ERR_INVALID_PARAM;
25 }
26 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
27 HDF_LOGE("%s: write reply failed", __func__);
28 return HDF_ERR_IO;
29 }
30 } else {
31 return HDF_ERR_NOT_SUPPORT;
32 }
33
34 return HDF_SUCCESS;
35 }
36
GpioTestReadConfig(struct GpioTestConfig * config,const struct DeviceResourceNode * node)37 static int32_t GpioTestReadConfig(struct GpioTestConfig *config, const struct DeviceResourceNode *node)
38 {
39 int32_t ret;
40 uint16_t tmp;
41 struct DeviceResourceIface *drsOps = NULL;
42
43 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
44 if (drsOps == NULL || drsOps->GetUint16 == NULL) {
45 HDF_LOGE("%s: invalid drs ops fail!", __func__);
46 return HDF_FAILURE;
47 }
48
49 ret = drsOps->GetUint16(node, "gpio", &tmp, 0);
50 if (ret != HDF_SUCCESS) {
51 HDF_LOGE("%s: read gpio fail!", __func__);
52 return ret;
53 }
54 config->gpio = (uint16_t)tmp;
55
56 ret = drsOps->GetUint16(node, "gpioIrq", &tmp, 0);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%s: read gpioIrq fail!", __func__);
59 return ret;
60 }
61 config->gpioIrq = (uint16_t)tmp;
62
63 ret = drsOps->GetUint16(node, "testUserApi", &tmp, 0);
64 if (ret != HDF_SUCCESS) {
65 HDF_LOGW("%s: read gpioIrq fail, using 0 as default", __func__);
66 config->testUserApi = 0;
67 }
68 config->testUserApi = (uint16_t)tmp;
69
70 return HDF_SUCCESS;
71 }
72
GpioTestBind(struct HdfDeviceObject * device)73 static int32_t GpioTestBind(struct HdfDeviceObject *device)
74 {
75 int32_t ret;
76 static struct IDeviceIoService service;
77
78 if (device == NULL || device->property == NULL) {
79 HDF_LOGE("%s: device or config is null!", __func__);
80 return HDF_ERR_IO;
81 }
82
83 ret = GpioTestReadConfig(&g_config, device->property);
84 if (ret != HDF_SUCCESS) {
85 HDF_LOGE("%s: read config failed", __func__);
86 return ret;
87 }
88
89 service.Dispatch = GpioTestDispatch;
90 device->service = &service;
91
92 return HDF_SUCCESS;
93 }
94
GpioTestInit(struct HdfDeviceObject * device)95 static int32_t GpioTestInit(struct HdfDeviceObject *device)
96 {
97 (void)device;
98 return HDF_SUCCESS;
99 }
100
GpioTestRelease(struct HdfDeviceObject * device)101 static void GpioTestRelease(struct HdfDeviceObject *device)
102 {
103 if (device != NULL) {
104 device->service = NULL;
105 }
106 return;
107 }
108
109 struct HdfDriverEntry g_gpioTestEntry = {
110 .moduleVersion = 1,
111 .Bind = GpioTestBind,
112 .Init = GpioTestInit,
113 .Release = GpioTestRelease,
114 .moduleName = "PLATFORM_GPIO_TEST",
115 };
116 HDF_INIT(g_gpioTestEntry);
117