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