xref: /openbmc/linux/drivers/mtd/chips/chipreg.c (revision e2c75e76)
1 /*
2  * Registration for chip drivers
3  *
4  */
5 
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>
13 
14 static DEFINE_SPINLOCK(chip_drvs_lock);
15 static LIST_HEAD(chip_drvs_list);
16 
17 void register_mtd_chip_driver(struct mtd_chip_driver *drv)
18 {
19 	spin_lock(&chip_drvs_lock);
20 	list_add(&drv->list, &chip_drvs_list);
21 	spin_unlock(&chip_drvs_lock);
22 }
23 
24 void unregister_mtd_chip_driver(struct mtd_chip_driver *drv)
25 {
26 	spin_lock(&chip_drvs_lock);
27 	list_del(&drv->list);
28 	spin_unlock(&chip_drvs_lock);
29 }
30 
31 static struct mtd_chip_driver *get_mtd_chip_driver (const char *name)
32 {
33 	struct list_head *pos;
34 	struct mtd_chip_driver *ret = NULL, *this;
35 
36 	spin_lock(&chip_drvs_lock);
37 
38 	list_for_each(pos, &chip_drvs_list) {
39 		this = list_entry(pos, typeof(*this), list);
40 
41 		if (!strcmp(this->name, name)) {
42 			ret = this;
43 			break;
44 		}
45 	}
46 	if (ret && !try_module_get(ret->module))
47 		ret = NULL;
48 
49 	spin_unlock(&chip_drvs_lock);
50 
51 	return ret;
52 }
53 
54 	/* Hide all the horrid details, like some silly person taking
55 	   get_module_symbol() away from us, from the caller. */
56 
57 struct mtd_info *do_map_probe(const char *name, struct map_info *map)
58 {
59 	struct mtd_chip_driver *drv;
60 	struct mtd_info *ret;
61 
62 	drv = get_mtd_chip_driver(name);
63 
64 	if (!drv && !request_module("%s", name))
65 		drv = get_mtd_chip_driver(name);
66 
67 	if (!drv)
68 		return NULL;
69 
70 	ret = drv->probe(map);
71 
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.
76 	*/
77 	module_put(drv->module);
78 
79 	return ret;
80 }
81 /*
82  * Destroy an MTD device which was created for a map device.
83  * Make sure the MTD device is already unregistered before calling this
84  */
85 void map_destroy(struct mtd_info *mtd)
86 {
87 	struct map_info *map = mtd->priv;
88 
89 	if (map->fldrv->destroy)
90 		map->fldrv->destroy(mtd);
91 
92 	module_put(map->fldrv->module);
93 
94 	kfree(mtd);
95 }
96 
97 EXPORT_SYMBOL(register_mtd_chip_driver);
98 EXPORT_SYMBOL(unregister_mtd_chip_driver);
99 EXPORT_SYMBOL(do_map_probe);
100 EXPORT_SYMBOL(map_destroy);
101 
102 MODULE_LICENSE("GPL");
103 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
104 MODULE_DESCRIPTION("Core routines for registering and invoking MTD chip drivers");
105