• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "securec.h"
10 
11 #include "hdf_base.h"
12 #include "hdf_log.h"
13 #include "mmc_block.h"
14 
15 #define HDF_LOG_TAG mmc_block_c
16 
MmcBlockInit(struct MmcDevice * mmcDevice)17 int32_t MmcBlockInit(struct MmcDevice *mmcDevice)
18 {
19     int32_t ret;
20     size_t nameSize;
21     struct MmcBlock *mb = NULL;
22 
23     if (mmcDevice == NULL) {
24         return HDF_ERR_INVALID_OBJECT;
25     }
26 
27     if (MmcDeviceGet(mmcDevice) == NULL) {
28         return HDF_PLT_ERR_DEV_GET;
29     }
30 
31     mb = (struct MmcBlock *)OsalMemCalloc(sizeof(*mb));
32     if (mb == NULL) {
33         PlatformDevicePut(&mmcDevice->device);
34         return HDF_ERR_MALLOC_FAIL;
35     }
36 
37     mmcDevice->mb = mb;
38     mb->mmc = mmcDevice;
39     mb->secSize = mmcDevice->secSize;
40     mb->capacity = mmcDevice->capacity;
41     mb->removeable = (mmcDevice->state.bits.removeable == 0) ? false : true;
42     nameSize = sizeof(mb->name);
43     ret = snprintf_s(mb->name, nameSize, nameSize - 1, "/dev/mmcblk%0d", mmcDevice->cntlr->index);
44     if (ret <= 0) {
45         OsalMemFree(mb);
46         PlatformDevicePut(&mmcDevice->device);
47         HDF_LOGE("%s: format block dev name failed, ret = %d", __func__, ret);
48         return HDF_PLT_ERR_OS_API;
49     }
50 
51     ret = MmcBlockOsInit(mmcDevice);
52     if (ret != HDF_SUCCESS) {
53         HDF_LOGE("%s: mmc block os init failed, ret = %d", __func__, ret);
54         OsalMemFree(mb);
55         PlatformDevicePut(&mmcDevice->device);
56         return ret;
57     }
58     return HDF_SUCCESS;
59 }
60 
MmcBlockUninit(struct MmcDevice * mmcDevice)61 void MmcBlockUninit(struct MmcDevice *mmcDevice)
62 {
63     if (mmcDevice != NULL) {
64         MmcBlockOsUninit(mmcDevice);
65         OsalMemFree(mmcDevice->mb);
66         mmcDevice->mb = NULL;
67         PlatformDevicePut(&mmcDevice->device);
68     }
69 }
70