Merge tag 'x86_urgent_for_v6.12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / mtd / mtdblock_ro.c
blobef6299af60e4f063bef9e8e3ca3b92990e07c9f0
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Simple read-only (writable only for RAM) mtdblock driver
5 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
6 */
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)
18 size_t retlen;
19 int err;
21 err = mtd_read(dev->mtd, (block * 512), 512, &retlen, buf);
22 if (err && !mtd_is_bitflip(err))
23 return 1;
24 return 0;
27 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
28 unsigned long block, char *buf)
30 size_t retlen;
32 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
33 return 1;
34 return 0;
37 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
39 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
41 if (!dev)
42 return;
44 dev->mtd = mtd;
45 dev->devnum = mtd->index;
47 dev->size = mtd->size >> 9;
48 dev->tr = tr;
49 dev->readonly = 1;
51 if (mtd_type_is_nand(mtd))
52 pr_warn_ratelimited("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
53 tr->name, mtd->name);
55 if (add_mtd_blktrans_dev(dev))
56 kfree(dev);
59 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
61 del_mtd_blktrans_dev(dev);
64 static struct mtd_blktrans_ops mtdblock_tr = {
65 .name = "mtdblock",
66 .major = MTD_BLOCK_MAJOR,
67 .part_bits = 0,
68 .blksize = 512,
69 .readsect = mtdblock_readsect,
70 .writesect = mtdblock_writesect,
71 .add_mtd = mtdblock_add_mtd,
72 .remove_dev = mtdblock_remove_dev,
73 .owner = THIS_MODULE,
76 module_mtd_blktrans(mtdblock_tr);
78 MODULE_LICENSE("GPL");
79 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
80 MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");