blob: 7fcf29ef2bdc4664ebf5520c254aee7219621ad1 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Simple read-only (writable only for RAM) mtdblock driver
David Woodhousea1452a32010-08-08 20:58:20 +01004 *
5 * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/mtd/mtd.h>
11#include <linux/mtd/blktrans.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040012#include <linux/module.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030013#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
16 unsigned long block, char *buf)
17{
18 size_t retlen;
19
Artem Bityutskiy329ad392011-12-23 17:30:16 +020020 if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 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
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +020030 if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 1;
32 return 0;
33}
34
35static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
36{
Burman Yan95b93a02006-11-15 21:10:29 +020037 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (!dev)
40 return;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 dev->mtd = mtd;
43 dev->devnum = mtd->index;
Richard Purdie19187672006-10-27 09:09:33 +010044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 dev->size = mtd->size >> 9;
46 dev->tr = tr;
Jörn Engelaf63a3b2006-04-13 18:53:55 +020047 dev->readonly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Maxim Levitsky298304f2010-02-22 20:39:31 +020049 if (add_mtd_blktrans_dev(dev))
50 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
54{
55 del_mtd_blktrans_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58static struct mtd_blktrans_ops mtdblock_tr = {
59 .name = "mtdblock",
Ezequiel Garcia2aabeb22013-10-08 20:59:08 -030060 .major = MTD_BLOCK_MAJOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .part_bits = 0,
Richard Purdie19187672006-10-27 09:09:33 +010062 .blksize = 512,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .readsect = mtdblock_readsect,
64 .writesect = mtdblock_writesect,
65 .add_mtd = mtdblock_add_mtd,
66 .remove_dev = mtdblock_remove_dev,
67 .owner = THIS_MODULE,
68};
69
70static int __init mtdblock_init(void)
71{
72 return register_mtd_blktrans(&mtdblock_tr);
73}
74
75static void __exit mtdblock_exit(void)
76{
77 deregister_mtd_blktrans(&mtdblock_tr);
78}
79
80module_init(mtdblock_init);
81module_exit(mtdblock_exit);
82
83MODULE_LICENSE("GPL");
84MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
85MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");