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