2 * $Id: mtdcore.c,v 1.42 2004/07/13 10:21:13 dwmw2 Exp $
4 * Core registration and callback routines for MTD
9 #include <linux/version.h>
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/ptrace.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/timer.h>
18 #include <linux/major.h>
20 #include <linux/ioctl.h>
21 #include <linux/init.h>
22 #include <linux/mtd/compatmac.h>
24 #include <linux/proc_fs.h>
27 #include <linux/mtd/mtd.h>
29 /* These are exported solely for the purpose of mtd_blkdevs.c. You
30 should not use them for _anything_ else */
31 DECLARE_MUTEX(mtd_table_mutex);
32 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
34 EXPORT_SYMBOL_GPL(mtd_table_mutex);
35 EXPORT_SYMBOL_GPL(mtd_table);
37 static LIST_HEAD(mtd_notifiers);
40 * add_mtd_device - register an MTD device
41 * @mtd: pointer to new MTD device info structure
43 * Add a device to the list of MTD devices present in the system, and
44 * notify each currently active MTD 'user' of its arrival. Returns
45 * zero on success or 1 on failure, which currently will only happen
46 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
49 int add_mtd_device(struct mtd_info *mtd)
53 down(&mtd_table_mutex);
55 for (i=0; i < MAX_MTD_DEVICES; i++)
57 struct list_head *this;
63 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
64 /* No need to get a refcount on the module containing
65 the notifier, since we hold the mtd_table_mutex */
66 list_for_each(this, &mtd_notifiers) {
67 struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
72 /* We _know_ we aren't being removed, because
73 our caller is still holding us here. So none
74 of this try_ nonsense, and no bitching about it
76 __module_get(THIS_MODULE);
85 * del_mtd_device - unregister an MTD device
86 * @mtd: pointer to MTD device info structure
88 * Remove a device from the list of MTD devices present in the system,
89 * and notify each currently active MTD 'user' of its departure.
90 * Returns zero on success or 1 on failure, which currently will happen
91 * if the requested device does not appear to be present in the list.
94 int del_mtd_device (struct mtd_info *mtd)
98 down(&mtd_table_mutex);
100 if (mtd_table[mtd->index] != mtd) {
102 } else if (mtd->usecount) {
103 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
104 mtd->index, mtd->name, mtd->usecount);
107 struct list_head *this;
109 /* No need to get a refcount on the module containing
110 the notifier, since we hold the mtd_table_mutex */
111 list_for_each(this, &mtd_notifiers) {
112 struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
116 mtd_table[mtd->index] = NULL;
118 module_put(THIS_MODULE);
122 up(&mtd_table_mutex);
127 * register_mtd_user - register a 'user' of MTD devices.
128 * @new: pointer to notifier info structure
130 * Registers a pair of callbacks function to be called upon addition
131 * or removal of MTD devices. Causes the 'add' callback to be immediately
132 * invoked for each MTD device currently present in the system.
135 void register_mtd_user (struct mtd_notifier *new)
139 down(&mtd_table_mutex);
141 list_add(&new->list, &mtd_notifiers);
143 __module_get(THIS_MODULE);
145 for (i=0; i< MAX_MTD_DEVICES; i++)
147 new->add(mtd_table[i]);
149 up(&mtd_table_mutex);
153 * register_mtd_user - unregister a 'user' of MTD devices.
154 * @new: pointer to notifier info structure
156 * Removes a callback function pair from the list of 'users' to be
157 * notified upon addition or removal of MTD devices. Causes the
158 * 'remove' callback to be immediately invoked for each MTD device
159 * currently present in the system.
162 int unregister_mtd_user (struct mtd_notifier *old)
166 down(&mtd_table_mutex);
168 module_put(THIS_MODULE);
170 for (i=0; i< MAX_MTD_DEVICES; i++)
172 old->remove(mtd_table[i]);
174 list_del(&old->list);
175 up(&mtd_table_mutex);
181 * get_mtd_device - obtain a validated handle for an MTD device
182 * @mtd: last known address of the required MTD device
183 * @num: internal device number of the required MTD device
185 * Given a number and NULL address, return the num'th entry in the device
186 * table, if any. Given an address and num == -1, search the device table
187 * for a device with that address and return if it's still present. Given
188 * both, return the num'th driver only if its address matches. Return NULL
192 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
194 struct mtd_info *ret = NULL;
197 down(&mtd_table_mutex);
200 for (i=0; i< MAX_MTD_DEVICES; i++)
201 if (mtd_table[i] == mtd)
203 } else if (num < MAX_MTD_DEVICES) {
204 ret = mtd_table[num];
205 if (mtd && mtd != ret)
209 if (ret && !try_module_get(ret->owner))
215 up(&mtd_table_mutex);
219 void put_mtd_device(struct mtd_info *mtd)
223 down(&mtd_table_mutex);
225 up(&mtd_table_mutex);
228 module_put(mtd->owner);
231 /* default_mtd_writev - default mtd writev method for MTD devices that
232 * dont implement their own
235 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
236 unsigned long count, loff_t to, size_t *retlen)
239 size_t totlen = 0, thislen;
245 for (i=0; i<count; i++) {
246 if (!vecs[i].iov_len)
248 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
250 if (ret || thislen != vecs[i].iov_len)
252 to += vecs[i].iov_len;
261 /* default_mtd_readv - default mtd readv method for MTD devices that dont
262 * implement their own
265 int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
266 unsigned long count, loff_t from, size_t *retlen)
269 size_t totlen = 0, thislen;
275 for (i=0; i<count; i++) {
276 if (!vecs[i].iov_len)
278 ret = mtd->read(mtd, from, vecs[i].iov_len, &thislen, vecs[i].iov_base);
280 if (ret || thislen != vecs[i].iov_len)
282 from += vecs[i].iov_len;
291 EXPORT_SYMBOL(add_mtd_device);
292 EXPORT_SYMBOL(del_mtd_device);
293 EXPORT_SYMBOL(get_mtd_device);
294 EXPORT_SYMBOL(put_mtd_device);
295 EXPORT_SYMBOL(register_mtd_user);
296 EXPORT_SYMBOL(unregister_mtd_user);
297 EXPORT_SYMBOL(default_mtd_writev);
298 EXPORT_SYMBOL(default_mtd_readv);
300 /*====================================================================*/
301 /* Power management code */
305 #include <linux/pm.h>
307 static struct pm_dev *mtd_pm_dev = NULL;
309 static int mtd_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
313 if (down_trylock(&mtd_table_mutex))
315 if (rqst == PM_SUSPEND) {
316 for (i = 0; ret == 0 && i < MAX_MTD_DEVICES; i++) {
317 if (mtd_table[i] && mtd_table[i]->suspend)
318 ret = mtd_table[i]->suspend(mtd_table[i]);
320 } else i = MAX_MTD_DEVICES-1;
322 if (rqst == PM_RESUME || ret) {
323 for ( ; i >= 0; i--) {
324 if (mtd_table[i] && mtd_table[i]->resume)
325 mtd_table[i]->resume(mtd_table[i]);
328 up(&mtd_table_mutex);
333 /*====================================================================*/
334 /* Support for /proc/mtd */
336 #ifdef CONFIG_PROC_FS
337 static struct proc_dir_entry *proc_mtd;
339 static inline int mtd_proc_info (char *buf, int i)
341 struct mtd_info *this = mtd_table[i];
346 return sprintf(buf, "mtd%d: %8.8x %8.8x \"%s\"\n", i, this->size,
347 this->erasesize, this->name);
350 static int mtd_read_proc (char *page, char **start, off_t off, int count,
351 int *eof, void *data_unused)
356 down(&mtd_table_mutex);
358 len = sprintf(page, "dev: size erasesize name\n");
359 for (i=0; i< MAX_MTD_DEVICES; i++) {
361 l = mtd_proc_info(page + len, i);
363 if (len+begin > off+count)
365 if (len+begin < off) {
374 up(&mtd_table_mutex);
375 if (off >= len+begin)
377 *start = page + (off-begin);
378 return ((count < begin+len-off) ? count : begin+len-off);
381 #endif /* CONFIG_PROC_FS */
383 /*====================================================================*/
386 int __init init_mtd(void)
388 #ifdef CONFIG_PROC_FS
389 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
390 proc_mtd->read_proc = mtd_read_proc;
394 mtd_pm_dev = pm_register(PM_UNKNOWN_DEV, 0, mtd_pm_callback);
399 static void __exit cleanup_mtd(void)
403 pm_unregister(mtd_pm_dev);
408 #ifdef CONFIG_PROC_FS
410 remove_proc_entry( "mtd", NULL);
414 module_init(init_mtd);
415 module_exit(cleanup_mtd);
418 MODULE_LICENSE("GPL");
419 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
420 MODULE_DESCRIPTION("Core MTD registration and access routines");