1 /*
2 * Copyright (c) 2020-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 "i2c_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 I2cTestConfig g_config;
16
I2cTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)17 static int32_t I2cTestDispatch(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
I2cTestReadConfig(struct I2cTestConfig * config,const struct DeviceResourceNode * node)37 static int32_t I2cTestReadConfig(struct I2cTestConfig *config, const struct DeviceResourceNode *node)
38 {
39 int32_t ret;
40 struct DeviceResourceIface *drsOps = NULL;
41
42 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
43 if (drsOps == NULL || drsOps->GetUint32 == NULL) {
44 HDF_LOGE("%s: invalid drs ops", __func__);
45 return HDF_FAILURE;
46 }
47
48 ret = drsOps->GetUint16(node, "bus_num", &config->busNum, 0);
49 if (ret != HDF_SUCCESS) {
50 HDF_LOGE("%s: read bus num failed", __func__);
51 return ret;
52 }
53
54 ret = drsOps->GetUint16(node, "dev_addr", &config->devAddr, 0);
55 if (ret != HDF_SUCCESS) {
56 HDF_LOGE("%s: read dev addr failed", __func__);
57 return ret;
58 }
59
60 ret = drsOps->GetUint16(node, "reg_len", &config->regLen, 1);
61 if (ret != HDF_SUCCESS) {
62 HDF_LOGE("%s: read reg len failed", __func__);
63 return ret;
64 }
65
66 ret = drsOps->GetUint16(node, "reg_addr", &config->regAddr, 0);
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("%s: read reg addr failed", __func__);
69 return ret;
70 }
71
72 ret = drsOps->GetUint16(node, "buf_size", &config->bufSize, 0);
73 if (ret != HDF_SUCCESS) {
74 HDF_LOGE("%s: read buf size failed", __func__);
75 return ret;
76 }
77
78 return HDF_SUCCESS;
79 }
80
I2cTestBind(struct HdfDeviceObject * device)81 static int32_t I2cTestBind(struct HdfDeviceObject *device)
82 {
83 int32_t ret;
84 static struct IDeviceIoService service;
85
86 if (device == NULL || device->property == NULL) {
87 HDF_LOGE("%s: device or config is null!", __func__);
88 return HDF_ERR_IO;
89 }
90
91 ret = I2cTestReadConfig(&g_config, device->property);
92 if (ret != HDF_SUCCESS) {
93 HDF_LOGE("%s: read config failed", __func__);
94 return ret;
95 }
96
97 service.Dispatch = I2cTestDispatch;
98 device->service = &service;
99
100 return HDF_SUCCESS;
101 }
102
I2cTestInit(struct HdfDeviceObject * device)103 static int32_t I2cTestInit(struct HdfDeviceObject *device)
104 {
105 (void)device;
106 return HDF_SUCCESS;
107 }
108
I2cTestRelease(struct HdfDeviceObject * device)109 static void I2cTestRelease(struct HdfDeviceObject *device)
110 {
111 if (device != NULL) {
112 device->service = NULL;
113 }
114 return;
115 }
116
117 struct HdfDriverEntry g_i2cTestEntry = {
118 .moduleVersion = 1,
119 .Bind = I2cTestBind,
120 .Init = I2cTestInit,
121 .Release = I2cTestRelease,
122 .moduleName = "PLATFORM_I2C_TEST",
123 };
124 HDF_INIT(g_i2cTestEntry);
125