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 "mmc_if.h"
10 #ifndef __USER__
11 #include "mmc_corex.h"
12 #endif
13 #ifdef __USER__
14 #include "hdf_io_service_if.h"
15 #endif
16 #include "hdf_log.h"
17 #include "osal_mem.h"
18 #include "securec.h"
19 
20 #define HDF_LOG_TAG mmc_if_c
21 
22 #define MMC_SVC_NAME_LEN 32
23 
MmcCntlrObjGetByNumber(int16_t id)24 static void *MmcCntlrObjGetByNumber(int16_t id)
25 {
26     void *object = NULL;
27     char *serviceName = NULL;
28 
29     if (id < 0) {
30         return NULL;
31     }
32     serviceName = OsalMemCalloc(MMC_SVC_NAME_LEN + 1);
33     if (serviceName == NULL) {
34         HDF_LOGE("MmcCntlrObjGetByNumber: OsalMemCalloc fail!");
35         return NULL;
36     }
37     if (snprintf_s(serviceName, MMC_SVC_NAME_LEN + 1, MMC_SVC_NAME_LEN,
38         "HDF_PLATFORM_MMC_%d", id) < 0) {
39         HDF_LOGE("MmcCntlrObjGetByNumber: format service name fail!");
40         OsalMemFree(serviceName);
41         return object;
42     }
43 
44 #ifdef __USER__
45     object = (void *)HdfIoServiceBind(serviceName);
46 #else
47     object = (void *)MmcCntlrGetByNr(id);
48 #endif
49     if (object == NULL) {
50         HDF_LOGE("MmcCntlrObjGetByNumber: get service fail!");
51     } else {
52         HDF_LOGD("MmcCntlrObjGetByNumber: success");
53     }
54 
55     OsalMemFree(serviceName);
56     return object;
57 }
58 
MmcOpen(int16_t id)59 DevHandle MmcOpen(int16_t id)
60 {
61     return (DevHandle)MmcCntlrObjGetByNumber(id);
62 }
63 
MmcClose(DevHandle handle)64 void MmcClose(DevHandle handle)
65 {
66     if (handle != NULL) {
67 #ifdef __USER__
68         HdfIoServiceRecycle((struct HdfIoService *)handle);
69 #endif
70     }
71 }
72 
MmcDevPresent(DevHandle handle)73 bool MmcDevPresent(DevHandle handle)
74 {
75     if (handle == NULL) {
76         return false;
77     }
78 #ifdef __USER__
79     int32_t ret;
80     uint8_t present = 0;
81     struct HdfSBuf *reply = NULL;
82     struct HdfIoService *service = (struct HdfIoService *)handle;
83 
84     reply = HdfSbufObtainDefaultSize();
85     if (reply == NULL) {
86         HDF_LOGE("MmcDevPresent: fail to obtain reply!");
87         return false;
88     }
89 
90     if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) {
91         HDF_LOGE("MmcDevPresent: dispatcher or Dispatch is null!");
92         goto __EXIT;
93     }
94     ret = service->dispatcher->Dispatch(&service->object, MMC_CMD_DEV_PRESENT,
95         NULL, reply);
96     if (ret != HDF_SUCCESS) {
97         HDF_LOGE("MmcDevPresent: failed to send service call, ret: %d!", ret);
98         goto __EXIT;
99     }
100 
101     if (HdfSbufReadUint8(reply, &present) == false) {
102         HDF_LOGE("MmcDevPresent: read present fail!");
103         goto __EXIT;
104     }
105 __EXIT:
106     HdfSbufRecycle(reply);
107     return (present != 0);
108 #else
109     return MmcCntlrDevPresent((struct MmcCntlr *)handle);
110 #endif
111 }
112