Linux 3.11-rc3
[cris-mirror.git] / drivers / mtd / mtdblock_ro.c
blob92759a9d2985e8a478b6a05734db195453d39434
1 /*
2 * Simple read-only (writable only for RAM) mtdblock driver
4 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/blktrans.h>
26 #include <linux/module.h>
28 static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
29 unsigned long block, char *buf)
31 size_t retlen;
33 if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
34 return 1;
35 return 0;
38 static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
39 unsigned long block, char *buf)
41 size_t retlen;
43 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
44 return 1;
45 return 0;
48 static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
50 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
52 if (!dev)
53 return;
55 dev->mtd = mtd;
56 dev->devnum = mtd->index;
58 dev->size = mtd->size >> 9;
59 dev->tr = tr;
60 dev->readonly = 1;
62 if (add_mtd_blktrans_dev(dev))
63 kfree(dev);
66 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
68 del_mtd_blktrans_dev(dev);
71 static struct mtd_blktrans_ops mtdblock_tr = {
72 .name = "mtdblock",
73 .major = 31,
74 .part_bits = 0,
75 .blksize = 512,
76 .readsect = mtdblock_readsect,
77 .writesect = mtdblock_writesect,
78 .add_mtd = mtdblock_add_mtd,
79 .remove_dev = mtdblock_remove_dev,
80 .owner = THIS_MODULE,
83 static int __init mtdblock_init(void)
85 return register_mtd_blktrans(&mtdblock_tr);
88 static void __exit mtdblock_exit(void)
90 deregister_mtd_blktrans(&mtdblock_tr);
93 module_init(mtdblock_init);
94 module_exit(mtdblock_exit);
96 MODULE_LICENSE("GPL");
97 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
98 MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");