Loading...
1/*
2 * Simple read-only (writable only for RAM) mtdblock driver
3 *
4 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/blktrans.h>
26
27static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
28 unsigned long block, char *buf)
29{
30 size_t retlen;
31
32 if (dev->mtd->read(dev->mtd, (block * 512), 512, &retlen, buf))
33 return 1;
34 return 0;
35}
36
37static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
38 unsigned long block, char *buf)
39{
40 size_t retlen;
41
42 if (dev->mtd->write(dev->mtd, (block * 512), 512, &retlen, buf))
43 return 1;
44 return 0;
45}
46
47static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
48{
49 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
50
51 if (!dev)
52 return;
53
54 dev->mtd = mtd;
55 dev->devnum = mtd->index;
56
57 dev->size = mtd->size >> 9;
58 dev->tr = tr;
59 dev->readonly = 1;
60
61 if (add_mtd_blktrans_dev(dev))
62 kfree(dev);
63}
64
65static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
66{
67 del_mtd_blktrans_dev(dev);
68}
69
70static struct mtd_blktrans_ops mtdblock_tr = {
71 .name = "mtdblock",
72 .major = 31,
73 .part_bits = 0,
74 .blksize = 512,
75 .readsect = mtdblock_readsect,
76 .writesect = mtdblock_writesect,
77 .add_mtd = mtdblock_add_mtd,
78 .remove_dev = mtdblock_remove_dev,
79 .owner = THIS_MODULE,
80};
81
82static int __init mtdblock_init(void)
83{
84 return register_mtd_blktrans(&mtdblock_tr);
85}
86
87static void __exit mtdblock_exit(void)
88{
89 deregister_mtd_blktrans(&mtdblock_tr);
90}
91
92module_init(mtdblock_init);
93module_exit(mtdblock_exit);
94
95MODULE_LICENSE("GPL");
96MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
97MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Simple read-only (writable only for RAM) mtdblock driver
4 *
5 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
6 */
7
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/mtd/mtd.h>
11#include <linux/mtd/blktrans.h>
12#include <linux/module.h>
13#include <linux/major.h>
14
15static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
16 unsigned long block, char *buf)
17{
18 size_t retlen;
19
20 if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
21 return 1;
22 return 0;
23}
24
25static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
26 unsigned long block, char *buf)
27{
28 size_t retlen;
29
30 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
31 return 1;
32 return 0;
33}
34
35static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
36{
37 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
38
39 if (!dev)
40 return;
41
42 dev->mtd = mtd;
43 dev->devnum = mtd->index;
44
45 dev->size = mtd->size >> 9;
46 dev->tr = tr;
47 dev->readonly = 1;
48
49 if (mtd_type_is_nand(mtd))
50 pr_warn("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
51 tr->name, mtd->name);
52
53 if (add_mtd_blktrans_dev(dev))
54 kfree(dev);
55}
56
57static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
58{
59 del_mtd_blktrans_dev(dev);
60}
61
62static struct mtd_blktrans_ops mtdblock_tr = {
63 .name = "mtdblock",
64 .major = MTD_BLOCK_MAJOR,
65 .part_bits = 0,
66 .blksize = 512,
67 .readsect = mtdblock_readsect,
68 .writesect = mtdblock_writesect,
69 .add_mtd = mtdblock_add_mtd,
70 .remove_dev = mtdblock_remove_dev,
71 .owner = THIS_MODULE,
72};
73
74module_mtd_blktrans(mtdblock_tr);
75
76MODULE_LICENSE("GPL");
77MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
78MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");