summaryrefslogtreecommitdiff
path: root/components/drivers/block/blk_dev.c
blob: 5c45aa5ecc3c57c2bd5d14069827e40d79435944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     first version
 */

#include "blk_dev.h"
#include "blk_dfs.h"

#define DBG_TAG "blk.dm"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#ifdef RT_USING_DFS
#include <dfs_fs.h>
#endif

static rt_err_t blk_dev_open(rt_device_t dev, rt_uint16_t oflag)
{
    struct rt_blk_device *blk = to_blk(dev);

    return rt_device_open(&blk->disk->parent, oflag);
}

static rt_err_t blk_dev_close(rt_device_t dev)
{
    struct rt_blk_device *blk = to_blk(dev);

    return rt_device_close(&blk->disk->parent);
}

static rt_ssize_t blk_dev_read(rt_device_t dev, rt_off_t sector,
        void *buffer, rt_size_t sector_count)
{
    struct rt_blk_device *blk = to_blk(dev);

    if (sector <= blk->sector_start + blk->sector_count &&
        sector_count <= blk->sector_count)
    {
        return rt_device_read(&blk->disk->parent,
                blk->sector_start + sector, buffer, sector_count);
    }

    return -RT_EINVAL;
}

static rt_ssize_t blk_dev_write(rt_device_t dev, rt_off_t sector,
        const void *buffer, rt_size_t sector_count)
{
    struct rt_blk_device *blk = to_blk(dev);

    if (sector <= blk->sector_start + blk->sector_count &&
        sector_count <= blk->sector_count)
    {
        return rt_device_write(&blk->disk->parent,
                blk->sector_start + sector, buffer, sector_count);
    }

    return -RT_EINVAL;
}

static rt_err_t blk_dev_control(rt_device_t dev, int cmd, void *args)
{
    rt_err_t err = -RT_EINVAL;
    struct rt_blk_device *blk = to_blk(dev);
    struct rt_blk_disk *disk = blk->disk;
    struct rt_device_blk_geometry disk_geometry, *geometry;

    switch (cmd)
    {
    case RT_DEVICE_CTRL_BLK_GETGEOME:
        if ((geometry = args))
        {
            if (!(err = disk->ops->getgeome(disk, &disk_geometry)))
            {
                geometry->bytes_per_sector = disk_geometry.bytes_per_sector;
                geometry->block_size = disk_geometry.block_size;
                geometry->sector_count = blk->sector_count;
            }
        }
        else
        {
            err = -RT_EINVAL;
        }

        break;

    case RT_DEVICE_CTRL_BLK_SYNC:
        rt_device_control(&disk->parent, cmd, args);
        break;

    case RT_DEVICE_CTRL_BLK_ERASE:
    case RT_DEVICE_CTRL_BLK_AUTOREFRESH:
        if (disk->partitions <= 1)
        {
            rt_device_control(&disk->parent, cmd, args);
        }
        else
        {
            err = -RT_EIO;
        }
        break;

    case RT_DEVICE_CTRL_BLK_PARTITION:
        if (args)
        {
            rt_memcpy(args, &blk->partition, sizeof(blk->partition));
        }
        else
        {
            err = -RT_EINVAL;
        }

        break;

    case RT_DEVICE_CTRL_BLK_SSIZEGET:
        device_get_blk_ssize(dev, args);
        err = RT_EOK;
        break;

    case RT_DEVICE_CTRL_ALL_BLK_SSIZEGET:
        device_get_all_blk_ssize(dev, args);
        err = RT_EOK;
        break;

    default:
        if (disk->ops->control)
        {
            err = disk->ops->control(disk, blk, cmd, args);
        }
        break;
    }

    return err;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops blk_dev_ops =
{
    .open = blk_dev_open,
    .close = blk_dev_close,
    .read = blk_dev_read,
    .write = blk_dev_write,
    .control = blk_dev_control,
};
#endif

rt_err_t blk_dev_initialize(struct rt_blk_device *blk)
{
    struct rt_device *dev;

    if (!blk)
    {
        return -RT_EINVAL;
    }

    dev = &blk->parent;
    dev->type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
    dev->ops = &blk_dev_ops;
#else
    dev->open = blk_dev_open;
    dev->close = blk_dev_close;
    dev->read = blk_dev_read;
    dev->write = blk_dev_write;
    dev->control = blk_dev_control;
#endif

    return RT_EOK;
}

rt_err_t disk_add_blk_dev(struct rt_blk_disk *disk, struct rt_blk_device *blk)
{
    rt_err_t err;
#ifdef RT_USING_DM
    int device_id;
#endif
    const char *disk_name, *name_fmt;

    if (!disk || !blk)
    {
        return -RT_EINVAL;
    }

#ifdef RT_USING_DM
    if ((device_id = rt_dm_ida_alloc(disk->ida)) < 0)
    {
        return -RT_EFULL;
    }
#endif

    blk->disk = disk;
    rt_list_init(&blk->list);

    disk_name = to_disk_name(disk);

    /* End is [a-zA-Z] or [0-9] */
    if (disk_name[rt_strlen(disk_name) - 1] < 'a')
    {
        name_fmt = "%sp%d";
    }
    else
    {
        name_fmt = "%s%d";
    }

#ifdef RT_USING_DM
    rt_dm_dev_set_name(&blk->parent, name_fmt, disk_name, blk->partno);
    blk->parent.master_id = disk->ida->master_id;
    blk->parent.device_id = device_id;
#else
    rt_snprintf(blk->parent.parent.name, RT_NAME_MAX, name_fmt, disk_name, blk->partno);
#endif
    device_set_blk_fops(&blk->parent);

    err = rt_device_register(&blk->parent, to_blk_name(blk),
            disk->parent.flag & RT_DEVICE_FLAG_RDWR);

    if (err)
    {
    #ifdef RT_USING_DM
        rt_dm_ida_free(disk->ida, device_id);
    #endif
        return err;
    }

    spin_lock(&disk->lock);

    rt_list_insert_before(&disk->part_nodes, &blk->list);

    spin_unlock(&disk->lock);

    return RT_EOK;
}

rt_err_t disk_remove_blk_dev(struct rt_blk_device *blk, rt_bool_t lockless)
{
    struct rt_blk_disk *disk;

    if (!blk)
    {
        return -RT_EINVAL;
    }

    disk = blk->disk;

    if (!disk)
    {
        return -RT_EINVAL;
    }
    else
    {
    #ifdef RT_USING_DFS
        const char *mountpath;

        if ((mountpath = dfs_filesystem_get_mounted_path(&blk->parent)))
        {
            dfs_unmount(mountpath);
            LOG_D("%s: Unmount file system on %s",
                    to_blk_name(blk), mountpath);
        }
    #endif
    }

#ifdef RT_USING_DM
    rt_dm_ida_free(disk->ida, blk->parent.device_id);
#endif

    rt_device_unregister(&blk->parent);

    if (!lockless)
    {
        spin_lock(&disk->lock);
    }

    rt_list_remove(&blk->list);

    if (!lockless)
    {
        spin_unlock(&disk->lock);
    }

    --disk->partitions;

    return RT_EOK;
}

rt_uint32_t blk_request_ioprio(void)
{
    struct rt_thread *task = rt_thread_self();

    return task ? RT_SCHED_PRIV(task).current_priority : 0;
}