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 "device_resource_if.h"
10 #include "hdf_base.h"
11 #include "hdf_log.h"
12 #include "osal_mem.h"
13 #include "osal_time.h"
14 #include "emmc_if.h"
15 #include "emmc_test.h"
16 
17 #define HDF_LOG_TAG emmc_test_c
18 
19 struct EmmcTestFunc {
20     enum EmmcTestCmd type;
21     int32_t (*Func)(struct EmmcTester *tester);
22 };
23 
EmmcTestGetHandle(struct EmmcTester * tester)24 static DevHandle EmmcTestGetHandle(struct EmmcTester *tester)
25 {
26     if (tester == NULL) {
27         HDF_LOGE("EmmcTestGetHandle: tester is null!");
28         return NULL;
29     }
30     return EmmcOpen(tester->busNum);
31 }
32 
EmmcTestReleaseHandle(DevHandle handle)33 static void EmmcTestReleaseHandle(DevHandle handle)
34 {
35     if (handle == NULL) {
36         HDF_LOGE("EmmcTestReleaseHandle: sdio handle is null!");
37         return;
38     }
39     EmmcClose(handle);
40 }
41 
TestEmmcGetCid(struct EmmcTester * tester)42 static int32_t TestEmmcGetCid(struct EmmcTester *tester)
43 {
44     int32_t ret;
45     int32_t i;
46     uint8_t cid[EMMC_CID_LEN] = {0};
47 
48     ret = EmmcGetCid(tester->handle, cid, EMMC_CID_LEN);
49     if (ret != HDF_SUCCESS) {
50         HDF_LOGE("TestEmmcGetCid: EmmcGetCid fail, ret = %d!", ret);
51         return HDF_FAILURE;
52     }
53     for (i = 0; i < EMMC_CID_LEN; i++) {
54         HDF_LOGE("TestEmmcGetCid: cid[%d] = 0x%x\n", i, cid[i]);
55     }
56     return HDF_SUCCESS;
57 }
58 
59 struct EmmcTestFunc g_emmcTestFunc[] = {
60     { EMMC_GET_CID_01, TestEmmcGetCid },
61 };
62 
EmmcTestEntry(struct EmmcTester * tester,int32_t cmd)63 static int32_t EmmcTestEntry(struct EmmcTester *tester, int32_t cmd)
64 {
65     int32_t i;
66     int32_t ret = HDF_SUCCESS;
67     bool isFind = false;
68 
69     if (tester == NULL) {
70         HDF_LOGE("EmmcTestEntry: tester is null!");
71         return HDF_ERR_INVALID_OBJECT;
72     }
73     tester->handle = EmmcTestGetHandle(tester);
74     if (tester->handle == NULL) {
75         HDF_LOGE("EmmcTestEntry: emmc test get handle fail!");
76         return HDF_FAILURE;
77     }
78     for (i = 0; i < sizeof(g_emmcTestFunc) / sizeof(g_emmcTestFunc[0]); i++) {
79         if (cmd == g_emmcTestFunc[i].type && g_emmcTestFunc[i].Func != NULL) {
80             ret = g_emmcTestFunc[i].Func(tester);
81             isFind = true;
82             break;
83         }
84     }
85     if (!isFind) {
86         ret = HDF_ERR_NOT_SUPPORT;
87         HDF_LOGE("EmmcTestEntry: cmd %d is not support!", cmd);
88     }
89     EmmcTestReleaseHandle(tester->handle);
90     return ret;
91 }
92 
EmmcTestFillConfig(struct EmmcTester * tester,const struct DeviceResourceNode * node)93 static int32_t EmmcTestFillConfig(struct EmmcTester *tester, const struct DeviceResourceNode *node)
94 {
95     int32_t ret;
96     struct DeviceResourceIface *drsOps = NULL;
97 
98     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
99     if (drsOps == NULL || drsOps->GetUint32 == NULL) {
100         HDF_LOGE("EmmcTestFillConfig: invalid drs ops!");
101         return HDF_FAILURE;
102     }
103 
104     ret = drsOps->GetUint32(node, "busNum", &(tester->busNum), 0);
105     if (ret != HDF_SUCCESS) {
106         HDF_LOGE("EmmcTestFillConfig: fill bus num fail!");
107         return ret;
108     }
109 
110     ret = drsOps->GetUint32(node, "hostId", &(tester->hostId), 0);
111     if (ret != HDF_SUCCESS) {
112         HDF_LOGE("EmmcTestFillConfig: fill hostId fail!");
113         return ret;
114     }
115 
116     HDF_LOGD("EmmcTestFillConfig: busNum:%d, hostId:%d!", tester->busNum, tester->hostId);
117     return HDF_SUCCESS;
118 }
119 
EmmcTestBind(struct HdfDeviceObject * device)120 static int32_t EmmcTestBind(struct HdfDeviceObject *device)
121 {
122     static struct EmmcTester tester;
123 
124     if (device == NULL) {
125         HDF_LOGE("EmmcTestBind: device or config is null!");
126         return HDF_ERR_IO;
127     }
128 
129     device->service = &tester.service;
130     HDF_LOGD("EmmcTestBind: EMMC_TEST service bind success!");
131     return HDF_SUCCESS;
132 }
133 
EmmcTestInit(struct HdfDeviceObject * device)134 static int32_t EmmcTestInit(struct HdfDeviceObject *device)
135 {
136     struct EmmcTester *tester = NULL;
137     int32_t ret;
138 
139     if (device == NULL || device->service == NULL || device->property == NULL) {
140         HDF_LOGE("EmmcTestInit: invalid parameter!");
141         return HDF_ERR_INVALID_PARAM;
142     }
143 
144     tester = (struct EmmcTester *)device->service;
145     ret = EmmcTestFillConfig(tester, device->property);
146     if (ret != HDF_SUCCESS) {
147         HDF_LOGE("EmmcTestInit: read config fail!");
148         return ret;
149     }
150     tester->TestEntry = EmmcTestEntry;
151     HDF_LOGD("EmmcTestInit: success!");
152     return HDF_SUCCESS;
153 }
154 
EmmcTestRelease(struct HdfDeviceObject * device)155 static void EmmcTestRelease(struct HdfDeviceObject *device)
156 {
157     if (device != NULL) {
158         HDF_LOGE("EmmcTestRelease: device is null!");
159         device->service = NULL;
160     }
161 }
162 
163 struct HdfDriverEntry g_emmcTestEntry = {
164     .moduleVersion = 1,
165     .Bind = EmmcTestBind,
166     .Init = EmmcTestInit,
167     .Release = EmmcTestRelease,
168     .moduleName = "PLATFORM_EMMC_TEST",
169 };
170 HDF_INIT(g_emmcTestEntry);
171