Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * (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 | |
| 12 | static 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 | |
| 22 | static 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 | |
| 32 | static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 33 | { |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 34 | struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | if (!dev) |
| 37 | return; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | dev->mtd = mtd; |
| 40 | dev->devnum = mtd->index; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | dev->size = mtd->size >> 9; |
| 43 | dev->tr = tr; |
Jörn Engel | af63a3b | 2006-04-13 18:53:55 +0200 | [diff] [blame] | 44 | dev->readonly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | add_mtd_blktrans_dev(dev); |
| 47 | } |
| 48 | |
| 49 | static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) |
| 50 | { |
| 51 | del_mtd_blktrans_dev(dev); |
| 52 | kfree(dev); |
| 53 | } |
| 54 | |
| 55 | static struct mtd_blktrans_ops mtdblock_tr = { |
| 56 | .name = "mtdblock", |
| 57 | .major = 31, |
| 58 | .part_bits = 0, |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 59 | .blksize = 512, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | .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 | |
| 67 | static int __init mtdblock_init(void) |
| 68 | { |
| 69 | return register_mtd_blktrans(&mtdblock_tr); |
| 70 | } |
| 71 | |
| 72 | static void __exit mtdblock_exit(void) |
| 73 | { |
| 74 | deregister_mtd_blktrans(&mtdblock_tr); |
| 75 | } |
| 76 | |
| 77 | module_init(mtdblock_init); |
| 78 | module_exit(mtdblock_exit); |
| 79 | |
| 80 | MODULE_LICENSE("GPL"); |
| 81 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 82 | MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices"); |