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