Thomas Gleixner | fd534e9 | 2019-05-23 11:14:39 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Simple read-only (writable only for RAM) mtdblock driver |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/mtd/mtd.h> |
| 11 | #include <linux/mtd/blktrans.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
Ezequiel Garcia | f83c383 | 2013-10-13 18:05:23 -0300 | [diff] [blame] | 13 | #include <linux/major.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | static int mtdblock_readsect(struct mtd_blktrans_dev *dev, |
| 16 | unsigned long block, char *buf) |
| 17 | { |
| 18 | size_t retlen; |
| 19 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 20 | if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | return 1; |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int mtdblock_writesect(struct mtd_blktrans_dev *dev, |
| 26 | unsigned long block, char *buf) |
| 27 | { |
| 28 | size_t retlen; |
| 29 | |
Artem Bityutskiy | eda95cb | 2011-12-23 17:35:41 +0200 | [diff] [blame] | 30 | if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | return 1; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 36 | { |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 37 | struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | if (!dev) |
| 40 | return; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | dev->mtd = mtd; |
| 43 | dev->devnum = mtd->index; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | dev->size = mtd->size >> 9; |
| 46 | dev->tr = tr; |
Jörn Engel | af63a3b | 2006-04-13 18:53:55 +0200 | [diff] [blame] | 47 | dev->readonly = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Ezequiel Garcia | e07403a | 2021-08-01 20:45:09 -0300 | [diff] [blame] | 49 | if (mtd_type_is_nand(mtd)) |
| 50 | pr_warn("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n", |
| 51 | tr->name, mtd->name); |
| 52 | |
Maxim Levitsky | 298304f | 2010-02-22 20:39:31 +0200 | [diff] [blame] | 53 | if (add_mtd_blktrans_dev(dev)) |
| 54 | kfree(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) |
| 58 | { |
| 59 | del_mtd_blktrans_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static struct mtd_blktrans_ops mtdblock_tr = { |
| 63 | .name = "mtdblock", |
Ezequiel Garcia | 2aabeb2 | 2013-10-08 20:59:08 -0300 | [diff] [blame] | 64 | .major = MTD_BLOCK_MAJOR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | .part_bits = 0, |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 66 | .blksize = 512, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | .readsect = mtdblock_readsect, |
| 68 | .writesect = mtdblock_writesect, |
| 69 | .add_mtd = mtdblock_add_mtd, |
| 70 | .remove_dev = mtdblock_remove_dev, |
| 71 | .owner = THIS_MODULE, |
| 72 | }; |
| 73 | |
Dejin Zheng | b1f9604 | 2021-02-14 00:45:57 +0800 | [diff] [blame] | 74 | module_mtd_blktrans(mtdblock_tr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | MODULE_LICENSE("GPL"); |
| 77 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 78 | MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices"); |