blob: 92759a9d2985e8a478b6a05734db195453d39434 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Simple read-only (writable only for RAM) mtdblock driver
David Woodhousea1452a32010-08-08 20:58:20 +01003 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/blktrans.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040026#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
29 unsigned long block, char *buf)
30{
31 size_t retlen;
32
Artem Bityutskiy329ad392011-12-23 17:30:16 +020033 if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return 1;
35 return 0;
36}
37
38static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
39 unsigned long block, char *buf)
40{
41 size_t retlen;
42
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +020043 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return 1;
45 return 0;
46}
47
48static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
49{
Burman Yan95b93a02006-11-15 21:10:29 +020050 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 if (!dev)
53 return;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 dev->mtd = mtd;
56 dev->devnum = mtd->index;
Richard Purdie19187672006-10-27 09:09:33 +010057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 dev->size = mtd->size >> 9;
59 dev->tr = tr;
Jörn Engelaf63a3b2006-04-13 18:53:55 +020060 dev->readonly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Maxim Levitsky298304f2010-02-22 20:39:31 +020062 if (add_mtd_blktrans_dev(dev))
63 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
67{
68 del_mtd_blktrans_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static struct mtd_blktrans_ops mtdblock_tr = {
72 .name = "mtdblock",
73 .major = 31,
74 .part_bits = 0,
Richard Purdie19187672006-10-27 09:09:33 +010075 .blksize = 512,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .readsect = mtdblock_readsect,
77 .writesect = mtdblock_writesect,
78 .add_mtd = mtdblock_add_mtd,
79 .remove_dev = mtdblock_remove_dev,
80 .owner = THIS_MODULE,
81};
82
83static int __init mtdblock_init(void)
84{
85 return register_mtd_blktrans(&mtdblock_tr);
86}
87
88static void __exit mtdblock_exit(void)
89{
90 deregister_mtd_blktrans(&mtdblock_tr);
91}
92
93module_init(mtdblock_init);
94module_exit(mtdblock_exit);
95
96MODULE_LICENSE("GPL");
97MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
98MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");