map_absent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common code to handle absent "placeholder" devices
  4. * Copyright 2001 Resilience Corporation <ebrower@resilience.com>
  5. *
  6. * This map driver is used to allocate "placeholder" MTD
  7. * devices on systems that have socketed/removable media.
  8. * Use of this driver as a fallback preserves the expected
  9. * registration of MTD device nodes regardless of probe outcome.
  10. * A usage example is as follows:
  11. *
  12. * my_dev[i] = do_map_probe("cfi", &my_map[i]);
  13. * if(NULL == my_dev[i]) {
  14. * my_dev[i] = do_map_probe("map_absent", &my_map[i]);
  15. * }
  16. *
  17. * Any device 'probed' with this driver will return -ENODEV
  18. * upon open.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/map.h>
  28. static int map_absent_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  29. static int map_absent_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  30. static int map_absent_erase (struct mtd_info *, struct erase_info *);
  31. static void map_absent_sync (struct mtd_info *);
  32. static struct mtd_info *map_absent_probe(struct map_info *map);
  33. static void map_absent_destroy (struct mtd_info *);
  34. static struct mtd_chip_driver map_absent_chipdrv = {
  35. .probe = map_absent_probe,
  36. .destroy = map_absent_destroy,
  37. .name = "map_absent",
  38. .module = THIS_MODULE
  39. };
  40. static struct mtd_info *map_absent_probe(struct map_info *map)
  41. {
  42. struct mtd_info *mtd;
  43. mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
  44. if (!mtd) {
  45. return NULL;
  46. }
  47. map->fldrv = &map_absent_chipdrv;
  48. mtd->priv = map;
  49. mtd->name = map->name;
  50. mtd->type = MTD_ABSENT;
  51. mtd->size = map->size;
  52. mtd->_erase = map_absent_erase;
  53. mtd->_read = map_absent_read;
  54. mtd->_write = map_absent_write;
  55. mtd->_sync = map_absent_sync;
  56. mtd->flags = 0;
  57. mtd->erasesize = PAGE_SIZE;
  58. mtd->writesize = 1;
  59. __module_get(THIS_MODULE);
  60. return mtd;
  61. }
  62. static int map_absent_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  63. {
  64. return -ENODEV;
  65. }
  66. static int map_absent_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  67. {
  68. return -ENODEV;
  69. }
  70. static int map_absent_erase(struct mtd_info *mtd, struct erase_info *instr)
  71. {
  72. return -ENODEV;
  73. }
  74. static void map_absent_sync(struct mtd_info *mtd)
  75. {
  76. /* nop */
  77. }
  78. static void map_absent_destroy(struct mtd_info *mtd)
  79. {
  80. /* nop */
  81. }
  82. static int __init map_absent_init(void)
  83. {
  84. register_mtd_chip_driver(&map_absent_chipdrv);
  85. return 0;
  86. }
  87. static void __exit map_absent_exit(void)
  88. {
  89. unregister_mtd_chip_driver(&map_absent_chipdrv);
  90. }
  91. module_init(map_absent_init);
  92. module_exit(map_absent_exit);
  93. MODULE_LICENSE("GPL");
  94. MODULE_AUTHOR("Resilience Corporation - Eric Brower <ebrower@resilience.com>");
  95. MODULE_DESCRIPTION("Placeholder MTD chip driver for 'absent' chips");