map_rom.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Common code to handle map devices which are simple ROM
  3. * (C) 2000 Red Hat. GPL'd.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/types.h>
  7. #include <linux/kernel.h>
  8. #include <asm/io.h>
  9. #include <asm/byteorder.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/map.h>
  15. #include <linux/mtd/compatmac.h>
  16. static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  17. static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  18. static void maprom_nop (struct mtd_info *);
  19. static struct mtd_info *map_rom_probe(struct map_info *map);
  20. static int maprom_erase (struct mtd_info *mtd, struct erase_info *info);
  21. static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
  22. unsigned long, unsigned long);
  23. static struct mtd_chip_driver maprom_chipdrv = {
  24. .probe = map_rom_probe,
  25. .name = "map_rom",
  26. .module = THIS_MODULE
  27. };
  28. static struct mtd_info *map_rom_probe(struct map_info *map)
  29. {
  30. struct mtd_info *mtd;
  31. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  32. if (!mtd)
  33. return NULL;
  34. map->fldrv = &maprom_chipdrv;
  35. mtd->priv = map;
  36. mtd->name = map->name;
  37. mtd->type = MTD_ROM;
  38. mtd->size = map->size;
  39. mtd->get_unmapped_area = maprom_unmapped_area;
  40. mtd->read = maprom_read;
  41. mtd->write = maprom_write;
  42. mtd->sync = maprom_nop;
  43. mtd->erase = maprom_erase;
  44. mtd->flags = MTD_CAP_ROM;
  45. mtd->erasesize = map->size;
  46. mtd->writesize = 1;
  47. __module_get(THIS_MODULE);
  48. return mtd;
  49. }
  50. /*
  51. * Allow NOMMU mmap() to directly map the device (if not NULL)
  52. * - return the address to which the offset maps
  53. * - return -ENOSYS to indicate refusal to do the mapping
  54. */
  55. static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
  56. unsigned long len,
  57. unsigned long offset,
  58. unsigned long flags)
  59. {
  60. struct map_info *map = mtd->priv;
  61. return (unsigned long) map->virt + offset;
  62. }
  63. static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  64. {
  65. struct map_info *map = mtd->priv;
  66. map_copy_from(map, buf, from, len);
  67. *retlen = len;
  68. return 0;
  69. }
  70. static void maprom_nop(struct mtd_info *mtd)
  71. {
  72. /* Nothing to see here */
  73. }
  74. static int maprom_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  75. {
  76. printk(KERN_NOTICE "maprom_write called\n");
  77. return -EIO;
  78. }
  79. static int maprom_erase (struct mtd_info *mtd, struct erase_info *info)
  80. {
  81. /* We do our best 8) */
  82. return -EROFS;
  83. }
  84. static int __init map_rom_init(void)
  85. {
  86. register_mtd_chip_driver(&maprom_chipdrv);
  87. return 0;
  88. }
  89. static void __exit map_rom_exit(void)
  90. {
  91. unregister_mtd_chip_driver(&maprom_chipdrv);
  92. }
  93. module_init(map_rom_init);
  94. module_exit(map_rom_exit);
  95. MODULE_LICENSE("GPL");
  96. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  97. MODULE_DESCRIPTION("MTD chip driver for ROM chips");