mtdblock_ro.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
  26. unsigned long block, char *buf)
  27. {
  28. size_t retlen;
  29. if (dev->mtd->read(dev->mtd, (block * 512), 512, &retlen, buf))
  30. return 1;
  31. return 0;
  32. }
  33. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  34. unsigned long block, char *buf)
  35. {
  36. size_t retlen;
  37. if (dev->mtd->write(dev->mtd, (block * 512), 512, &retlen, buf))
  38. return 1;
  39. return 0;
  40. }
  41. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  42. {
  43. struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  44. if (!dev)
  45. return;
  46. dev->mtd = mtd;
  47. dev->devnum = mtd->index;
  48. dev->size = mtd->size >> 9;
  49. dev->tr = tr;
  50. dev->readonly = 1;
  51. if (add_mtd_blktrans_dev(dev))
  52. kfree(dev);
  53. }
  54. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  55. {
  56. del_mtd_blktrans_dev(dev);
  57. }
  58. static struct mtd_blktrans_ops mtdblock_tr = {
  59. .name = "mtdblock",
  60. .major = 31,
  61. .part_bits = 0,
  62. .blksize = 512,
  63. .readsect = mtdblock_readsect,
  64. .writesect = mtdblock_writesect,
  65. .add_mtd = mtdblock_add_mtd,
  66. .remove_dev = mtdblock_remove_dev,
  67. .owner = THIS_MODULE,
  68. };
  69. static int __init mtdblock_init(void)
  70. {
  71. return register_mtd_blktrans(&mtdblock_tr);
  72. }
  73. static void __exit mtdblock_exit(void)
  74. {
  75. deregister_mtd_blktrans(&mtdblock_tr);
  76. }
  77. module_init(mtdblock_init);
  78. module_exit(mtdblock_exit);
  79. MODULE_LICENSE("GPL");
  80. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  81. MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");