| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * Registration for chip drivers | 
 | 3 |  * | 
 | 4 |  */ | 
 | 5 |  | 
 | 6 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> | 
 | 8 | #include <linux/kmod.h> | 
 | 9 | #include <linux/spinlock.h> | 
 | 10 | #include <linux/slab.h> | 
 | 11 | #include <linux/mtd/map.h> | 
 | 12 | #include <linux/mtd/mtd.h> | 
 | 13 | #include <linux/mtd/compatmac.h> | 
 | 14 |  | 
 | 15 | static DEFINE_SPINLOCK(chip_drvs_lock); | 
 | 16 | static LIST_HEAD(chip_drvs_list); | 
 | 17 |  | 
 | 18 | void register_mtd_chip_driver(struct mtd_chip_driver *drv) | 
 | 19 | { | 
 | 20 | 	spin_lock(&chip_drvs_lock); | 
 | 21 | 	list_add(&drv->list, &chip_drvs_list); | 
 | 22 | 	spin_unlock(&chip_drvs_lock); | 
 | 23 | } | 
 | 24 |  | 
 | 25 | void unregister_mtd_chip_driver(struct mtd_chip_driver *drv) | 
 | 26 | { | 
 | 27 | 	spin_lock(&chip_drvs_lock); | 
 | 28 | 	list_del(&drv->list); | 
 | 29 | 	spin_unlock(&chip_drvs_lock); | 
 | 30 | } | 
 | 31 |  | 
 | 32 | static struct mtd_chip_driver *get_mtd_chip_driver (const char *name) | 
 | 33 | { | 
 | 34 | 	struct list_head *pos; | 
 | 35 | 	struct mtd_chip_driver *ret = NULL, *this; | 
 | 36 |  | 
 | 37 | 	spin_lock(&chip_drvs_lock); | 
 | 38 |  | 
 | 39 | 	list_for_each(pos, &chip_drvs_list) { | 
 | 40 | 		this = list_entry(pos, typeof(*this), list); | 
| Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 41 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | 		if (!strcmp(this->name, name)) { | 
 | 43 | 			ret = this; | 
 | 44 | 			break; | 
 | 45 | 		} | 
 | 46 | 	} | 
 | 47 | 	if (ret && !try_module_get(ret->module)) | 
 | 48 | 		ret = NULL; | 
 | 49 |  | 
 | 50 | 	spin_unlock(&chip_drvs_lock); | 
 | 51 |  | 
 | 52 | 	return ret; | 
 | 53 | } | 
 | 54 |  | 
 | 55 | 	/* Hide all the horrid details, like some silly person taking | 
 | 56 | 	   get_module_symbol() away from us, from the caller. */ | 
 | 57 |  | 
 | 58 | struct mtd_info *do_map_probe(const char *name, struct map_info *map) | 
 | 59 | { | 
 | 60 | 	struct mtd_chip_driver *drv; | 
 | 61 | 	struct mtd_info *ret; | 
 | 62 |  | 
 | 63 | 	drv = get_mtd_chip_driver(name); | 
 | 64 |  | 
 | 65 | 	if (!drv && !request_module("%s", name)) | 
 | 66 | 		drv = get_mtd_chip_driver(name); | 
 | 67 |  | 
 | 68 | 	if (!drv) | 
 | 69 | 		return NULL; | 
 | 70 |  | 
 | 71 | 	ret = drv->probe(map); | 
 | 72 |  | 
| Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 73 | 	/* We decrease the use count here. It may have been a | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | 	   probe-only module, which is no longer required from this | 
 | 75 | 	   point, having given us a handle on (and increased the use | 
 | 76 | 	   count of) the actual driver code. | 
 | 77 | 	*/ | 
 | 78 | 	module_put(drv->module); | 
 | 79 |  | 
 | 80 | 	if (ret) | 
 | 81 | 		return ret; | 
| Thomas Gleixner | 1f948b4 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 82 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | 	return NULL; | 
 | 84 | } | 
 | 85 | /* | 
 | 86 |  * Destroy an MTD device which was created for a map device. | 
 | 87 |  * Make sure the MTD device is already unregistered before calling this | 
 | 88 |  */ | 
 | 89 | void map_destroy(struct mtd_info *mtd) | 
 | 90 | { | 
 | 91 | 	struct map_info *map = mtd->priv; | 
 | 92 |  | 
 | 93 | 	if (map->fldrv->destroy) | 
 | 94 | 		map->fldrv->destroy(mtd); | 
 | 95 |  | 
 | 96 | 	module_put(map->fldrv->module); | 
 | 97 |  | 
 | 98 | 	kfree(mtd); | 
 | 99 | } | 
 | 100 |  | 
 | 101 | EXPORT_SYMBOL(register_mtd_chip_driver); | 
 | 102 | EXPORT_SYMBOL(unregister_mtd_chip_driver); | 
 | 103 | EXPORT_SYMBOL(do_map_probe); | 
 | 104 | EXPORT_SYMBOL(map_destroy); | 
 | 105 |  | 
 | 106 | MODULE_LICENSE("GPL"); | 
 | 107 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | 
 | 108 | MODULE_DESCRIPTION("Core routines for registering and invoking MTD chip drivers"); |