2 * Core registration and callback routines for MTD
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/ptrace.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
12 #include <linux/timer.h>
13 #include <linux/major.h>
15 #include <linux/err.h>
16 #include <linux/ioctl.h>
17 #include <linux/init.h>
18 #include <linux/mtd/compatmac.h>
19 #include <linux/proc_fs.h>
20 #include <linux/idr.h>
22 #include <linux/mtd/mtd.h>
27 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
28 static int mtd_cls_resume(struct device *dev);
30 static struct class mtd_class = {
33 .suspend = mtd_cls_suspend,
34 .resume = mtd_cls_resume,
37 static DEFINE_IDR(mtd_idr);
39 /* These are exported solely for the purpose of mtd_blkdevs.c. You
40 should not use them for _anything_ else */
41 DEFINE_MUTEX(mtd_table_mutex);
42 EXPORT_SYMBOL_GPL(mtd_table_mutex);
44 struct mtd_info *__mtd_next_device(int i)
46 return idr_get_next(&mtd_idr, &i);
48 EXPORT_SYMBOL_GPL(__mtd_next_device);
50 static LIST_HEAD(mtd_notifiers);
53 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
54 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
56 #define MTD_DEVT(index) 0
59 /* REVISIT once MTD uses the driver model better, whoever allocates
60 * the mtd_info will probably want to use the release() hook...
62 static void mtd_release(struct device *dev)
64 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
66 /* remove /dev/mtdXro node if needed */
68 device_destroy(&mtd_class, index + 1);
71 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
73 struct mtd_info *mtd = dev_to_mtd(dev);
75 if (mtd && mtd->suspend)
76 return mtd->suspend(mtd);
81 static int mtd_cls_resume(struct device *dev)
83 struct mtd_info *mtd = dev_to_mtd(dev);
85 if (mtd && mtd->resume)
90 static ssize_t mtd_type_show(struct device *dev,
91 struct device_attribute *attr, char *buf)
93 struct mtd_info *mtd = dev_to_mtd(dev);
122 return snprintf(buf, PAGE_SIZE, "%s\n", type);
124 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
126 static ssize_t mtd_flags_show(struct device *dev,
127 struct device_attribute *attr, char *buf)
129 struct mtd_info *mtd = dev_to_mtd(dev);
131 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
134 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
136 static ssize_t mtd_size_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
139 struct mtd_info *mtd = dev_to_mtd(dev);
141 return snprintf(buf, PAGE_SIZE, "%llu\n",
142 (unsigned long long)mtd->size);
145 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
147 static ssize_t mtd_erasesize_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
150 struct mtd_info *mtd = dev_to_mtd(dev);
152 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
155 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
157 static ssize_t mtd_writesize_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct mtd_info *mtd = dev_to_mtd(dev);
162 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
165 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
167 static ssize_t mtd_subpagesize_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct mtd_info *mtd = dev_to_mtd(dev);
171 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
173 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
176 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
178 static ssize_t mtd_oobsize_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 struct mtd_info *mtd = dev_to_mtd(dev);
183 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
186 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
188 static ssize_t mtd_numeraseregions_show(struct device *dev,
189 struct device_attribute *attr, char *buf)
191 struct mtd_info *mtd = dev_to_mtd(dev);
193 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
196 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
199 static ssize_t mtd_name_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
202 struct mtd_info *mtd = dev_to_mtd(dev);
204 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
207 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
209 static struct attribute *mtd_attrs[] = {
211 &dev_attr_flags.attr,
213 &dev_attr_erasesize.attr,
214 &dev_attr_writesize.attr,
215 &dev_attr_subpagesize.attr,
216 &dev_attr_oobsize.attr,
217 &dev_attr_numeraseregions.attr,
222 static struct attribute_group mtd_group = {
226 static const struct attribute_group *mtd_groups[] = {
231 static struct device_type mtd_devtype = {
233 .groups = mtd_groups,
234 .release = mtd_release,
238 * add_mtd_device - register an MTD device
239 * @mtd: pointer to new MTD device info structure
241 * Add a device to the list of MTD devices present in the system, and
242 * notify each currently active MTD 'user' of its arrival. Returns
243 * zero on success or 1 on failure, which currently will only happen
244 * if there is insufficient memory or a sysfs error.
247 int add_mtd_device(struct mtd_info *mtd)
249 struct mtd_notifier *not;
252 if (!mtd->backing_dev_info) {
255 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
258 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
261 mtd->backing_dev_info = &mtd_bdi_unmappable;
266 BUG_ON(mtd->writesize == 0);
267 mutex_lock(&mtd_table_mutex);
270 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
272 error = idr_get_new(&mtd_idr, mtd, &i);
273 } while (error == -EAGAIN);
281 if (is_power_of_2(mtd->erasesize))
282 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
284 mtd->erasesize_shift = 0;
286 if (is_power_of_2(mtd->writesize))
287 mtd->writesize_shift = ffs(mtd->writesize) - 1;
289 mtd->writesize_shift = 0;
291 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
292 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
294 /* Some chips always power up locked. Unlock them now */
295 if ((mtd->flags & MTD_WRITEABLE)
296 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
297 if (mtd->unlock(mtd, 0, mtd->size))
299 "%s: unlock failed, writes may not work\n",
303 /* Caller should have set dev.parent to match the
306 mtd->dev.type = &mtd_devtype;
307 mtd->dev.class = &mtd_class;
308 mtd->dev.devt = MTD_DEVT(i);
309 dev_set_name(&mtd->dev, "mtd%d", i);
310 dev_set_drvdata(&mtd->dev, mtd);
311 if (device_register(&mtd->dev) != 0)
315 device_create(&mtd_class, mtd->dev.parent,
319 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
320 /* No need to get a refcount on the module containing
321 the notifier, since we hold the mtd_table_mutex */
322 list_for_each_entry(not, &mtd_notifiers, list)
325 mutex_unlock(&mtd_table_mutex);
326 /* We _know_ we aren't being removed, because
327 our caller is still holding us here. So none
328 of this try_ nonsense, and no bitching about it
330 __module_get(THIS_MODULE);
334 idr_remove(&mtd_idr, i);
336 mutex_unlock(&mtd_table_mutex);
341 * del_mtd_device - unregister an MTD device
342 * @mtd: pointer to MTD device info structure
344 * Remove a device from the list of MTD devices present in the system,
345 * and notify each currently active MTD 'user' of its departure.
346 * Returns zero on success or 1 on failure, which currently will happen
347 * if the requested device does not appear to be present in the list.
350 int del_mtd_device (struct mtd_info *mtd)
354 mutex_lock(&mtd_table_mutex);
356 if (idr_find(&mtd_idr, mtd->index) != mtd) {
358 } else if (mtd->usecount) {
359 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
360 mtd->index, mtd->name, mtd->usecount);
363 struct mtd_notifier *not;
365 device_unregister(&mtd->dev);
367 /* No need to get a refcount on the module containing
368 the notifier, since we hold the mtd_table_mutex */
369 list_for_each_entry(not, &mtd_notifiers, list)
372 idr_remove(&mtd_idr, mtd->index);
374 module_put(THIS_MODULE);
378 mutex_unlock(&mtd_table_mutex);
383 * register_mtd_user - register a 'user' of MTD devices.
384 * @new: pointer to notifier info structure
386 * Registers a pair of callbacks function to be called upon addition
387 * or removal of MTD devices. Causes the 'add' callback to be immediately
388 * invoked for each MTD device currently present in the system.
391 void register_mtd_user (struct mtd_notifier *new)
393 struct mtd_info *mtd;
395 mutex_lock(&mtd_table_mutex);
397 list_add(&new->list, &mtd_notifiers);
399 __module_get(THIS_MODULE);
401 mtd_for_each_device(mtd)
404 mutex_unlock(&mtd_table_mutex);
408 * unregister_mtd_user - unregister a 'user' of MTD devices.
409 * @old: pointer to notifier info structure
411 * Removes a callback function pair from the list of 'users' to be
412 * notified upon addition or removal of MTD devices. Causes the
413 * 'remove' callback to be immediately invoked for each MTD device
414 * currently present in the system.
417 int unregister_mtd_user (struct mtd_notifier *old)
419 struct mtd_info *mtd;
421 mutex_lock(&mtd_table_mutex);
423 module_put(THIS_MODULE);
425 mtd_for_each_device(mtd)
428 list_del(&old->list);
429 mutex_unlock(&mtd_table_mutex);
435 * get_mtd_device - obtain a validated handle for an MTD device
436 * @mtd: last known address of the required MTD device
437 * @num: internal device number of the required MTD device
439 * Given a number and NULL address, return the num'th entry in the device
440 * table, if any. Given an address and num == -1, search the device table
441 * for a device with that address and return if it's still present. Given
442 * both, return the num'th driver only if its address matches. Return
446 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
448 struct mtd_info *ret = NULL, *other;
451 mutex_lock(&mtd_table_mutex);
454 mtd_for_each_device(other) {
460 } else if (num >= 0) {
461 ret = idr_find(&mtd_idr, num);
462 if (mtd && mtd != ret)
469 if (!try_module_get(ret->owner))
472 if (ret->get_device) {
473 err = ret->get_device(ret);
479 mutex_unlock(&mtd_table_mutex);
483 module_put(ret->owner);
485 mutex_unlock(&mtd_table_mutex);
490 * get_mtd_device_nm - obtain a validated handle for an MTD device by
492 * @name: MTD device name to open
494 * This function returns MTD device description structure in case of
495 * success and an error code in case of failure.
498 struct mtd_info *get_mtd_device_nm(const char *name)
501 struct mtd_info *mtd = NULL, *other;
503 mutex_lock(&mtd_table_mutex);
505 mtd_for_each_device(other) {
506 if (!strcmp(name, other->name)) {
515 if (!try_module_get(mtd->owner))
518 if (mtd->get_device) {
519 err = mtd->get_device(mtd);
525 mutex_unlock(&mtd_table_mutex);
529 module_put(mtd->owner);
531 mutex_unlock(&mtd_table_mutex);
535 void put_mtd_device(struct mtd_info *mtd)
539 mutex_lock(&mtd_table_mutex);
542 mtd->put_device(mtd);
543 mutex_unlock(&mtd_table_mutex);
546 module_put(mtd->owner);
549 /* default_mtd_writev - default mtd writev method for MTD devices that
550 * don't implement their own
553 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
554 unsigned long count, loff_t to, size_t *retlen)
557 size_t totlen = 0, thislen;
563 for (i=0; i<count; i++) {
564 if (!vecs[i].iov_len)
566 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
568 if (ret || thislen != vecs[i].iov_len)
570 to += vecs[i].iov_len;
578 EXPORT_SYMBOL_GPL(add_mtd_device);
579 EXPORT_SYMBOL_GPL(del_mtd_device);
580 EXPORT_SYMBOL_GPL(get_mtd_device);
581 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
582 EXPORT_SYMBOL_GPL(put_mtd_device);
583 EXPORT_SYMBOL_GPL(register_mtd_user);
584 EXPORT_SYMBOL_GPL(unregister_mtd_user);
585 EXPORT_SYMBOL_GPL(default_mtd_writev);
587 #ifdef CONFIG_PROC_FS
589 /*====================================================================*/
590 /* Support for /proc/mtd */
592 static struct proc_dir_entry *proc_mtd;
594 static inline int mtd_proc_info(char *buf, struct mtd_info *this)
596 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
597 (unsigned long long)this->size,
598 this->erasesize, this->name);
601 static int mtd_read_proc (char *page, char **start, off_t off, int count,
602 int *eof, void *data_unused)
604 struct mtd_info *mtd;
608 mutex_lock(&mtd_table_mutex);
610 len = sprintf(page, "dev: size erasesize name\n");
611 mtd_for_each_device(mtd) {
612 l = mtd_proc_info(page + len, mtd);
614 if (len+begin > off+count)
616 if (len+begin < off) {
625 mutex_unlock(&mtd_table_mutex);
626 if (off >= len+begin)
628 *start = page + (off-begin);
629 return ((count < begin+len-off) ? count : begin+len-off);
632 #endif /* CONFIG_PROC_FS */
634 /*====================================================================*/
637 static int __init init_mtd(void)
640 ret = class_register(&mtd_class);
643 pr_err("Error registering mtd class: %d\n", ret);
646 #ifdef CONFIG_PROC_FS
647 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
648 proc_mtd->read_proc = mtd_read_proc;
649 #endif /* CONFIG_PROC_FS */
653 static void __exit cleanup_mtd(void)
655 #ifdef CONFIG_PROC_FS
657 remove_proc_entry( "mtd", NULL);
658 #endif /* CONFIG_PROC_FS */
659 class_unregister(&mtd_class);
662 module_init(init_mtd);
663 module_exit(cleanup_mtd);
665 MODULE_LICENSE("GPL");
666 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
667 MODULE_DESCRIPTION("Core MTD registration and access routines");