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