• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "fs/fs.h"
32 #include "hdf_log.h"
33 #include "mtd_block.h"
34 #include "mtd_core.h"
35 #include "sys/ioctl.h"
36 
37 #define HDF_LOG_TAG mtd_block_lite_c
38 
LiteosMtdBlockOpen(FAR struct Vnode * vnode)39 static int LiteosMtdBlockOpen(FAR struct Vnode *vnode)
40 {
41     (void)vnode;
42     return 0;
43 }
44 
LiteosMtdBlockClose(FAR struct Vnode * vnode)45 static int LiteosMtdBlockClose(FAR struct Vnode *vnode)
46 {
47     (void)vnode;
48     return 0;
49 }
50 
LiteosMtdBlockRead(FAR struct Vnode * vnode,FAR unsigned char * buf,unsigned long long secStart,unsigned int nSecs)51 static ssize_t LiteosMtdBlockRead(FAR struct Vnode *vnode, FAR unsigned char *buf,
52     unsigned long long secStart, unsigned int nSecs)
53 {
54     (void)vnode;
55     (void)buf;
56     (void)secStart;
57     (void)nSecs;
58     return 0;
59 }
60 
LiteosMtdBlockWrite(FAR struct Vnode * vnode,FAR const unsigned char * buf,unsigned long long secStart,unsigned int nSecs)61 static ssize_t LiteosMtdBlockWrite(FAR struct Vnode *vnode, FAR const unsigned char *buf,
62     unsigned long long secStart, unsigned int nSecs)
63 {
64     (void)vnode;
65     (void)buf;
66     (void)secStart;
67     (void)nSecs;
68     return 0;
69 }
70 
LiteosMtdBlockGeometry(FAR struct Vnode * vnode,FAR struct geometry * geometry)71 static int LiteosMtdBlockGeometry(FAR struct Vnode *vnode, FAR struct geometry *geometry)
72 {
73     (void)vnode;
74     (void)geometry;
75     return 0;
76 }
77 
LiteosMtdBlockIoctl(FAR struct Vnode * vnode,int cmd,unsigned long arg)78 static int32_t LiteosMtdBlockIoctl(FAR struct Vnode *vnode, int cmd, unsigned long arg)
79 {
80     (void)vnode;
81     (void)cmd;
82     (void)arg;
83     return 0;
84 }
85 
86 static struct block_operations g_liteosNandOps = {
87     .open = LiteosMtdBlockOpen,
88     .close = LiteosMtdBlockClose,
89     .read = LiteosMtdBlockRead,
90     .write = LiteosMtdBlockWrite,
91     .geometry = LiteosMtdBlockGeometry,
92     .ioctl = LiteosMtdBlockIoctl,
93     .unlink = NULL,
94 };
95 
96 static struct block_operations g_liteosSpinorOps = {
97     .open = LiteosMtdBlockOpen,
98     .close = LiteosMtdBlockClose,
99     .read = LiteosMtdBlockRead,
100     .write = LiteosMtdBlockWrite,
101     .geometry = LiteosMtdBlockGeometry,
102     .ioctl = LiteosMtdBlockIoctl,
103     .unlink = NULL,
104 };
105 
GetDevNandOps(void)106 struct block_operations *GetDevNandOps(void)
107 {
108     return &g_liteosNandOps;
109 }
110 
GetDevSpinorOps(void)111 struct block_operations *GetDevSpinorOps(void)
112 {
113     return &g_liteosSpinorOps;
114 }
115 
116 #define MTD_LITE_BLOCK_DRV_MODE 0755
MtdBlockOsInit(struct MtdDevice * mtdDevice)117 int32_t MtdBlockOsInit(struct MtdDevice *mtdDevice)
118 {
119     int ret;
120     const char *devPath = NULL;
121 
122     if (mtdDevice == NULL) {
123         HDF_LOGE("MtdBlockOsInit: register block dev(%s) fail, ret: %d!", devPath, ret);
124         return HDF_ERR_INVALID_OBJECT;
125     }
126 
127     if (mtdDevice->type == MTD_TYPE_SPI_NOR) {
128         devPath = "/dev/spinor";
129     } else if (mtdDevice->type == MTD_TYPE_SPI_NAND) {
130         devPath = "/dev/nand";
131     } else {
132         return HDF_ERR_NOT_SUPPORT;
133     }
134 
135     ret = register_blockdriver(devPath, GetDevNandOps(), MTD_LITE_BLOCK_DRV_MODE, mtdDevice);
136     if (ret != 0) {
137         HDF_LOGE("MtdBlockOsInit: register block dev(%s) fail, ret: %d!", devPath, ret);
138         return HDF_PLT_ERR_OS_API;
139     }
140     HDF_LOGI("MtdBlockOsInit: register block dev(%s) success!", devPath);
141     return HDF_SUCCESS;
142 }
143 
MtdBlockOsUninit(struct MtdDevice * mtdDevice)144 void MtdBlockOsUninit(struct MtdDevice *mtdDevice)
145 {
146     (void)mtdDevice;
147 }
148 
149