1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Simple read-only (writable only for RAM) mtdblock driver
5 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
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>
15 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
16 unsigned long block, char *buf)
20 if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
25 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
26 unsigned long block, char *buf)
30 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
35 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
37 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
43 dev->devnum = mtd->index;
45 dev->size = mtd->size >> 9;
49 if (mtd_type_is_nand(mtd))
50 pr_warn("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
53 if (add_mtd_blktrans_dev(dev))
57 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
59 del_mtd_blktrans_dev(dev);
62 static struct mtd_blktrans_ops mtdblock_tr = {
64 .major = MTD_BLOCK_MAJOR,
67 .readsect = mtdblock_readsect,
68 .writesect = mtdblock_writesect,
69 .add_mtd = mtdblock_add_mtd,
70 .remove_dev = mtdblock_remove_dev,
74 module_mtd_blktrans(mtdblock_tr);
76 MODULE_LICENSE("GPL");
77 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
78 MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");