Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / mtd / chips / chipreg.c
blobda1f96f385c70b695f6e52696cc91b3bf6833948
1 /*
2 * Registration for chip drivers
4 */
6 #include <linux/kernel.h>
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>
14 static DEFINE_SPINLOCK(chip_drvs_lock);
15 static LIST_HEAD(chip_drvs_list);
17 void register_mtd_chip_driver(struct mtd_chip_driver *drv)
19 spin_lock(&chip_drvs_lock);
20 list_add(&drv->list, &chip_drvs_list);
21 spin_unlock(&chip_drvs_lock);
24 void unregister_mtd_chip_driver(struct mtd_chip_driver *drv)
26 spin_lock(&chip_drvs_lock);
27 list_del(&drv->list);
28 spin_unlock(&chip_drvs_lock);
31 static struct mtd_chip_driver *get_mtd_chip_driver (const char *name)
33 struct list_head *pos;
34 struct mtd_chip_driver *ret = NULL, *this;
36 spin_lock(&chip_drvs_lock);
38 list_for_each(pos, &chip_drvs_list) {
39 this = list_entry(pos, typeof(*this), list);
41 if (!strcmp(this->name, name)) {
42 ret = this;
43 break;
46 if (ret && !try_module_get(ret->module))
47 ret = NULL;
49 spin_unlock(&chip_drvs_lock);
51 return ret;
54 /* Hide all the horrid details, like some silly person taking
55 get_module_symbol() away from us, from the caller. */
57 struct mtd_info *do_map_probe(const char *name, struct map_info *map)
59 struct mtd_chip_driver *drv;
60 struct mtd_info *ret;
62 drv = get_mtd_chip_driver(name);
64 if (!drv && !request_module("%s", name))
65 drv = get_mtd_chip_driver(name);
67 if (!drv)
68 return NULL;
70 ret = drv->probe(map);
72 /* We decrease the use count here. It may have been a
73 probe-only module, which is no longer required from this
74 point, having given us a handle on (and increased the use
75 count of) the actual driver code.
77 module_put(drv->module);
79 if (ret)
80 return ret;
82 return NULL;
85 * Destroy an MTD device which was created for a map device.
86 * Make sure the MTD device is already unregistered before calling this
88 void map_destroy(struct mtd_info *mtd)
90 struct map_info *map = mtd->priv;
92 if (map->fldrv->destroy)
93 map->fldrv->destroy(mtd);
95 module_put(map->fldrv->module);
97 kfree(mtd);
100 EXPORT_SYMBOL(register_mtd_chip_driver);
101 EXPORT_SYMBOL(unregister_mtd_chip_driver);
102 EXPORT_SYMBOL(do_map_probe);
103 EXPORT_SYMBOL(map_destroy);
105 MODULE_LICENSE("GPL");
106 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
107 MODULE_DESCRIPTION("Core routines for registering and invoking MTD chip drivers");