MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / chips / chipreg.c
blob3271b4edfd3bc595ed09e15946d3ff72852a7faf
1 /*
2 * $Id: chipreg.c,v 1.16 2003/05/29 09:36:15 dwmw2 Exp $
4 * Registration for chip drivers
6 */
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>
18 spinlock_t chip_drvs_lock = SPIN_LOCK_UNLOCKED;
19 static LIST_HEAD(chip_drvs_list);
21 void register_mtd_chip_driver(struct mtd_chip_driver *drv)
23 spin_lock(&chip_drvs_lock);
24 list_add(&drv->list, &chip_drvs_list);
25 spin_unlock(&chip_drvs_lock);
28 void unregister_mtd_chip_driver(struct mtd_chip_driver *drv)
30 spin_lock(&chip_drvs_lock);
31 list_del(&drv->list);
32 spin_unlock(&chip_drvs_lock);
35 static struct mtd_chip_driver *get_mtd_chip_driver (const char *name)
37 struct list_head *pos;
38 struct mtd_chip_driver *ret = NULL, *this;
40 spin_lock(&chip_drvs_lock);
42 list_for_each(pos, &chip_drvs_list) {
43 this = list_entry(pos, typeof(*this), list);
45 if (!strcmp(this->name, name)) {
46 ret = this;
47 break;
50 if (ret && !try_module_get(ret->module))
51 ret = NULL;
53 spin_unlock(&chip_drvs_lock);
55 return ret;
58 static int mtd_generic_point(struct mtd_info *mtd, loff_t from, size_t len,
59 size_t *retlen, u_char **mtdbuf)
61 struct map_info *map = (struct map_info *) mtd->priv;
62 *mtdbuf = (u_char *) (map->virt + ((int) from));
63 *retlen = len;
64 return(0);
67 static void mtd_generic_unpoint (struct mtd_info *mtd, u_char *addr,
68 loff_t from, size_t len)
70 /* I don't know what we should do here */
73 /* Hide all the horrid details, like some silly person taking
74 get_module_symbol() away from us, from the caller. */
76 struct mtd_info *do_map_probe(const char *name, struct map_info *map)
78 struct mtd_chip_driver *drv;
79 struct mtd_info *ret;
81 drv = get_mtd_chip_driver(name);
83 if (!drv && !request_module("%s", name))
84 drv = get_mtd_chip_driver(name);
86 if (!drv)
87 return NULL;
89 ret = drv->probe(map);
91 /* We decrease the use count here. It may have been a
92 probe-only module, which is no longer required from this
93 point, having given us a handle on (and increased the use
94 count of) the actual driver code.
96 module_put(drv->module);
98 if (ret) {
99 if (!ret->point)
100 ret->point = mtd_generic_point;
101 if (!ret->unpoint)
102 ret->unpoint = mtd_generic_unpoint;
103 return ret;
106 return NULL;
109 * Destroy an MTD device which was created for a map device.
110 * Make sure the MTD device is already unregistered before calling this
112 void map_destroy(struct mtd_info *mtd)
114 struct map_info *map = mtd->priv;
116 if (map->fldrv->destroy)
117 map->fldrv->destroy(mtd);
119 module_put(map->fldrv->module);
121 kfree(mtd);
124 EXPORT_SYMBOL(register_mtd_chip_driver);
125 EXPORT_SYMBOL(unregister_mtd_chip_driver);
126 EXPORT_SYMBOL(do_map_probe);
127 EXPORT_SYMBOL(map_destroy);
129 MODULE_LICENSE("GPL");
130 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
131 MODULE_DESCRIPTION("Core routines for registering and invoking MTD chip drivers");