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_emmc.h"
10 #include "securec.h"
11 
12 #define HDF_LOG_TAG mmc_emmc_c
13 
EmmcDeviceDefaultGetCid(struct EmmcDevice * dev,uint8_t * cid,uint32_t len)14 static int32_t EmmcDeviceDefaultGetCid(struct EmmcDevice *dev, uint8_t *cid, uint32_t len)
15 {
16     struct MmcDevice *mmc = (struct MmcDevice *)dev;
17 
18     if (memcpy_s(cid, sizeof(uint8_t) * len, (uint8_t *)(mmc->reg.rawCid),
19         sizeof(mmc->reg.rawCid)) != EOK) {
20         HDF_LOGE("EmmcDeviceDefaultGetCid: memcpy_s fail, size = %d!", len);
21         return HDF_FAILURE;
22     }
23     return HDF_SUCCESS;
24 }
25 
26 static struct EmmcDeviceOps g_emmcOps = {
27     .getCid = EmmcDeviceDefaultGetCid,
28 };
29 
EmmcDeviceGetCid(struct EmmcDevice * dev,uint8_t * cid,uint32_t len)30 int32_t EmmcDeviceGetCid(struct EmmcDevice *dev, uint8_t *cid, uint32_t len)
31 {
32     if (dev == NULL) {
33         HDF_LOGE("EmmcDeviceGetCid: dev is null!");
34         return HDF_ERR_INVALID_OBJECT;
35     }
36     if (cid == NULL || len == 0) {
37         HDF_LOGE("EmmcDeviceGetCid: cid is null or len is invalid!");
38         return HDF_ERR_INVALID_PARAM;
39     }
40     if (dev->emmcOps == NULL || dev->emmcOps->getCid == NULL) {
41         HDF_LOGE("EmmcDeviceGetCid: emmcOps or getCid is null!");
42         return HDF_ERR_INVALID_OBJECT;
43     }
44     return dev->emmcOps->getCid(dev, cid, len);
45 }
46 
EmmcDeviceAddOps(struct EmmcDevice * dev,struct EmmcDeviceOps * ops)47 void EmmcDeviceAddOps(struct EmmcDevice *dev, struct EmmcDeviceOps *ops)
48 {
49     if (dev == NULL) {
50         HDF_LOGE("EmmcDeviceAddOps: dev is null!");
51         return;
52     }
53     if (ops == NULL) {
54         dev->emmcOps = &g_emmcOps;
55         HDF_LOGD("EmmcDeviceAddOps: use default ops.");
56     } else {
57         dev->emmcOps = ops;
58     }
59 }
60