blob: 852165f8b1c3510720cfc03ad80c2e68b4cbd728 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 2003 David Woodhouse <dwmw2@infradead.org>
3 *
4 * Simple read-only (writable only for RAM) mtdblock driver
5 */
6
7#include <linux/init.h>
8#include <linux/slab.h>
9#include <linux/mtd/mtd.h>
10#include <linux/mtd/blktrans.h>
11
12static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
13 unsigned long block, char *buf)
14{
15 size_t retlen;
16
17 if (dev->mtd->read(dev->mtd, (block * 512), 512, &retlen, buf))
18 return 1;
19 return 0;
20}
21
22static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
23 unsigned long block, char *buf)
24{
25 size_t retlen;
26
27 if (dev->mtd->write(dev->mtd, (block * 512), 512, &retlen, buf))
28 return 1;
29 return 0;
30}
31
32static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
33{
Burman Yan95b93a02006-11-15 21:10:29 +020034 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 if (!dev)
37 return;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 dev->mtd = mtd;
40 dev->devnum = mtd->index;
Richard Purdie19187672006-10-27 09:09:33 +010041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 dev->size = mtd->size >> 9;
43 dev->tr = tr;
Jörn Engelaf63a3b2006-04-13 18:53:55 +020044 dev->readonly = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 add_mtd_blktrans_dev(dev);
47}
48
49static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
50{
51 del_mtd_blktrans_dev(dev);
52 kfree(dev);
53}
54
55static struct mtd_blktrans_ops mtdblock_tr = {
56 .name = "mtdblock",
57 .major = 31,
58 .part_bits = 0,
Richard Purdie19187672006-10-27 09:09:33 +010059 .blksize = 512,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .readsect = mtdblock_readsect,
61 .writesect = mtdblock_writesect,
62 .add_mtd = mtdblock_add_mtd,
63 .remove_dev = mtdblock_remove_dev,
64 .owner = THIS_MODULE,
65};
66
67static int __init mtdblock_init(void)
68{
69 return register_mtd_blktrans(&mtdblock_tr);
70}
71
72static void __exit mtdblock_exit(void)
73{
74 deregister_mtd_blktrans(&mtdblock_tr);
75}
76
77module_init(mtdblock_init);
78module_exit(mtdblock_exit);
79
80MODULE_LICENSE("GPL");
81MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
82MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");