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 "i3c_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 #define HDF_LOG_TAG i3c_driver_test_c
16
17 static struct I3cTestConfig g_config;
18
I3cTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t I3cTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21 HDF_LOGD("%s: enter!", __func__);
22
23 if (cmd == 0) {
24 if (reply == NULL) {
25 HDF_LOGE("%s: reply is null!", __func__);
26 return HDF_ERR_INVALID_PARAM;
27 }
28 if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
29 HDF_LOGE("%s: write reply failed", __func__);
30 return HDF_ERR_IO;
31 }
32 } else {
33 return HDF_ERR_NOT_SUPPORT;
34 }
35
36 return HDF_SUCCESS;
37 }
38
I3cTestReadConfig(struct I3cTestConfig * config,const struct DeviceResourceNode * node)39 static int32_t I3cTestReadConfig(struct I3cTestConfig *config, const struct DeviceResourceNode *node)
40 {
41 int32_t ret;
42 struct DeviceResourceIface *drsOps = NULL;
43
44 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
45 if (drsOps == NULL || drsOps->GetUint16 == NULL) {
46 HDF_LOGE("%s: invalid drs ops", __func__);
47 return HDF_FAILURE;
48 }
49
50 ret = drsOps->GetUint16(node, "busId", &config->busId, 0);
51 if (ret != HDF_SUCCESS) {
52 HDF_LOGE("%s: read busId failed", __func__);
53 return ret;
54 }
55
56 ret = drsOps->GetUint16(node, "devAddr", &config->devAddr, 0);
57 if (ret != HDF_SUCCESS) {
58 HDF_LOGE("%s: read dev addr failed", __func__);
59 return ret;
60 }
61
62 ret = drsOps->GetUint16(node, "regLen", &config->regLen, 1);
63 if (ret != HDF_SUCCESS) {
64 HDF_LOGE("%s: read reg len failed", __func__);
65 return ret;
66 }
67
68 ret = drsOps->GetUint16(node, "regAddr", &config->regAddr, 0);
69 if (ret != HDF_SUCCESS) {
70 HDF_LOGE("%s: read reg addr failed", __func__);
71 return ret;
72 }
73
74 ret = drsOps->GetUint16(node, "bufSize", &config->bufSize, 0);
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%s: read buf size failed", __func__);
77 return ret;
78 }
79 return HDF_SUCCESS;
80 }
81
I3cTestBind(struct HdfDeviceObject * device)82 static int32_t I3cTestBind(struct HdfDeviceObject *device)
83 {
84 int32_t ret;
85 static struct IDeviceIoService service;
86
87 if (device == NULL || device->property == NULL) {
88 HDF_LOGE("%s: device or config is null!", __func__);
89 return HDF_ERR_IO;
90 }
91
92 ret = I3cTestReadConfig(&g_config, device->property);
93 if (ret != HDF_SUCCESS) {
94 HDF_LOGE("%s: read config failed", __func__);
95 return ret;
96 }
97 service.Dispatch = I3cTestDispatch;
98 device->service = &service;
99 HDF_LOGI("%s: Done!", __func__);
100 return HDF_SUCCESS;
101 }
102
I3cTestInit(struct HdfDeviceObject * device)103 static int32_t I3cTestInit(struct HdfDeviceObject *device)
104 {
105 (void)device;
106 HDF_LOGI("%s: Done!", __func__);
107 return HDF_SUCCESS;
108 }
109
I3cTestRelease(struct HdfDeviceObject * device)110 static void I3cTestRelease(struct HdfDeviceObject *device)
111 {
112 if (device != NULL) {
113 device->service = NULL;
114 }
115 HDF_LOGI("%s: Done!", __func__);
116 return;
117 }
118
119 struct HdfDriverEntry g_i3cTestEntry = {
120 .moduleVersion = 1,
121 .Bind = I3cTestBind,
122 .Init = I3cTestInit,
123 .Release = I3cTestRelease,
124 .moduleName = "PLATFORM_I3C_TEST",
125 };
126 HDF_INIT(g_i3cTestEntry);
127