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