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 | * Simple read-only (writable only for RAM) mtdblock driver |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 3 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/mtd/mtd.h> |
| 25 | #include <linux/mtd/blktrans.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Ezequiel Garcia | f83c383 | 2013-10-13 18:05:23 -0300 | [diff] [blame] | 27 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | static int mtdblock_readsect(struct mtd_blktrans_dev *dev, |
| 30 | unsigned long block, char *buf) |
| 31 | { |
| 32 | size_t retlen; |
| 33 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 34 | if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | return 1; |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int mtdblock_writesect(struct mtd_blktrans_dev *dev, |
| 40 | unsigned long block, char *buf) |
| 41 | { |
| 42 | size_t retlen; |
| 43 | |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 44 | if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return 1; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 50 | { |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 51 | struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | if (!dev) |
| 54 | return; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | dev->mtd = mtd; |
| 57 | dev->devnum = mtd->index; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | dev->size = mtd->size >> 9; |
| 60 | dev->tr = tr; |
Jörn Engel | af63a3b | 2006-04-13 18:53:55 +0200 | [diff] [blame] | 61 | dev->readonly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Maxim Levitsky | 298304f | 2010-02-22 20:39:31 +0200 | [diff] [blame] | 63 | if (add_mtd_blktrans_dev(dev)) |
| 64 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) |
| 68 | { |
| 69 | del_mtd_blktrans_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static struct mtd_blktrans_ops mtdblock_tr = { |
| 73 | .name = "mtdblock", |
Ezequiel Garcia | 2aabeb2 | 2013-10-08 20:59:08 -0300 | [diff] [blame] | 74 | .major = MTD_BLOCK_MAJOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | .part_bits = 0, |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 76 | .blksize = 512, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | .readsect = mtdblock_readsect, |
| 78 | .writesect = mtdblock_writesect, |
| 79 | .add_mtd = mtdblock_add_mtd, |
| 80 | .remove_dev = mtdblock_remove_dev, |
| 81 | .owner = THIS_MODULE, |
| 82 | }; |
| 83 | |
| 84 | static int __init mtdblock_init(void) |
| 85 | { |
| 86 | return register_mtd_blktrans(&mtdblock_tr); |
| 87 | } |
| 88 | |
| 89 | static void __exit mtdblock_exit(void) |
| 90 | { |
| 91 | deregister_mtd_blktrans(&mtdblock_tr); |
| 92 | } |
| 93 | |
| 94 | module_init(mtdblock_init); |
| 95 | module_exit(mtdblock_exit); |
| 96 | |
| 97 | MODULE_LICENSE("GPL"); |
| 98 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 99 | MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices"); |