mtdblock_ro.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Simple read-only (writable only for RAM) mtdblock driver
  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. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/blktrans.h>
  25. #include <linux/module.h>
  26. static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
  27. unsigned long block, char *buf)
  28. {
  29. size_t retlen;
  30. if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
  31. return 1;
  32. return 0;
  33. }
  34. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  35. unsigned long block, char *buf)
  36. {
  37. size_t retlen;
  38. if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
  39. return 1;
  40. return 0;
  41. }
  42. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  43. {
  44. struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  45. if (!dev)
  46. return;
  47. dev->mtd = mtd;
  48. dev->devnum = mtd->index;
  49. dev->size = mtd->size >> 9;
  50. dev->tr = tr;
  51. dev->readonly = 1;
  52. if (add_mtd_blktrans_dev(dev))
  53. kfree(dev);
  54. }
  55. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  56. {
  57. del_mtd_blktrans_dev(dev);
  58. }
  59. static struct mtd_blktrans_ops mtdblock_tr = {
  60. .name = "mtdblock",
  61. .major = 31,
  62. .part_bits = 0,
  63. .blksize = 512,
  64. .readsect = mtdblock_readsect,
  65. .writesect = mtdblock_writesect,
  66. .add_mtd = mtdblock_add_mtd,
  67. .remove_dev = mtdblock_remove_dev,
  68. .owner = THIS_MODULE,
  69. };
  70. static int __init mtdblock_init(void)
  71. {
  72. return register_mtd_blktrans(&mtdblock_tr);
  73. }
  74. static void __exit mtdblock_exit(void)
  75. {
  76. deregister_mtd_blktrans(&mtdblock_tr);
  77. }
  78. module_init(mtdblock_init);
  79. module_exit(mtdblock_exit);
  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");