1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "device_resource_if.h"
10 #include "hdf_base.h"
11 #include "hdf_device_desc.h"
12 #include "hdf_ibus_intf.h"
13 #include "hdf_log.h"
14 #include "pcie_bus_test.h"
15 
16 static struct PcieBusTestConfig g_config;
17 
PcieBusTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)18 static int32_t PcieBusTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data,
19                                    struct HdfSBuf *reply)
20 {
21     HDF_LOGD("%s: enter!", __func__);
22 
23     (void)client;
24     (void)data;
25     if (cmd == 0) {
26         if (reply == NULL) {
27             HDF_LOGE("%s: reply is null!", __func__);
28             return HDF_ERR_INVALID_PARAM;
29         }
30         if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
31             HDF_LOGE("%s: write config failed", __func__);
32             return HDF_ERR_IO;
33         }
34         return HDF_SUCCESS;
35     } else {
36         return HDF_ERR_NOT_SUPPORT;
37     }
38 }
39 
PcieBusTestReadConfig(const struct DeviceResourceNode * node)40 static int32_t PcieBusTestReadConfig(const struct DeviceResourceNode *node)
41 {
42     HDF_LOGI("PcieBusTestReadConfig enter.");
43     int32_t ret;
44     uint32_t busNum = 0;
45     struct DeviceResourceIface *drsOps = NULL;
46 
47     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
48     if (drsOps == NULL || drsOps->GetUint32 == NULL) {
49         HDF_LOGE("PcieBusTestReadConfig: invalid drs ops");
50         return HDF_FAILURE;
51     }
52     ret = drsOps->GetUint32(node, "busNum", &busNum, 0);
53     if (ret != HDF_SUCCESS) {
54         HDF_LOGE("PcieBusTestReadConfig: read bus num failed");
55         return ret;
56     }
57     g_config.busNum = (uint8_t)busNum;
58     return HDF_SUCCESS;
59 }
60 
PcieBusTestBind(struct HdfDeviceObject * device)61 static int32_t PcieBusTestBind(struct HdfDeviceObject *device)
62 {
63     HDF_LOGI("%{public}s enter.", __func__);
64     static struct IDeviceIoService service;
65 
66     if (device == NULL) {
67         HDF_LOGE("%s: device or config is null!", __func__);
68         return HDF_ERR_IO;
69     }
70     service.Dispatch = PcieBusTestDispatch;
71     device->service = &service;
72 
73     return HDF_SUCCESS;
74 }
75 
PcieBusTestInit(struct HdfDeviceObject * device)76 static int32_t PcieBusTestInit(struct HdfDeviceObject *device)
77 {
78     int32_t ret;
79     if (device->property == NULL) {
80         HDF_LOGW("PcieBusTestInit property is NULL.");
81         return HDF_SUCCESS;
82     }
83 
84     ret = PcieBusTestReadConfig(device->property);
85     if (ret != HDF_SUCCESS) {
86         HDF_LOGE("%s: read config failed", __func__);
87     }
88     return ret;
89 }
90 
PcieBusTestRelease(struct HdfDeviceObject * device)91 static void PcieBusTestRelease(struct HdfDeviceObject *device)
92 {
93     if (device != NULL) {
94         device->service = NULL;
95     }
96     return;
97 }
98 
99 struct HdfDriverEntry g_pcieBusTestEntry = {
100     .moduleVersion = 1,
101     .Bind = PcieBusTestBind,
102     .Init = PcieBusTestInit,
103     .Release = PcieBusTestRelease,
104     .moduleName = "PLATFORM_PCIE_BUS_TEST",
105 };
106 HDF_INIT(g_pcieBusTestEntry);
107