2 * Core registration and callback routines for MTD
6 * Copyright © 2006 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/ptrace.h>
13 #include <linux/string.h>
14 #include <linux/timer.h>
15 #include <linux/major.h>
17 #include <linux/err.h>
18 #include <linux/ioctl.h>
19 #include <linux/init.h>
20 #include <linux/mtd/compatmac.h>
21 #include <linux/proc_fs.h>
22 #include <linux/idr.h>
23 #include <linux/backing-dev.h>
25 #include <linux/mtd/mtd.h>
29 * backing device capabilities for non-mappable devices (such as NAND flash)
30 * - permits private mappings, copies are taken of the data
32 struct backing_dev_info mtd_bdi_unmappable = {
33 .capabilities = BDI_CAP_MAP_COPY,
37 * backing device capabilities for R/O mappable devices (such as ROM)
38 * - permits private mappings, copies are taken of the data
39 * - permits non-writable shared mappings
41 struct backing_dev_info mtd_bdi_ro_mappable = {
42 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
43 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
47 * backing device capabilities for writable mappable devices (such as RAM)
48 * - permits private mappings, copies are taken of the data
49 * - permits non-writable shared mappings
51 struct backing_dev_info mtd_bdi_rw_mappable = {
52 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
53 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
57 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
58 static int mtd_cls_resume(struct device *dev);
60 static struct class mtd_class = {
63 .suspend = mtd_cls_suspend,
64 .resume = mtd_cls_resume,
67 static DEFINE_IDR(mtd_idr);
69 /* These are exported solely for the purpose of mtd_blkdevs.c. You
70 should not use them for _anything_ else */
71 DEFINE_MUTEX(mtd_table_mutex);
72 EXPORT_SYMBOL_GPL(mtd_table_mutex);
74 struct mtd_info *__mtd_next_device(int i)
76 return idr_get_next(&mtd_idr, &i);
78 EXPORT_SYMBOL_GPL(__mtd_next_device);
80 static LIST_HEAD(mtd_notifiers);
83 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
84 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
86 #define MTD_DEVT(index) 0
89 /* REVISIT once MTD uses the driver model better, whoever allocates
90 * the mtd_info will probably want to use the release() hook...
92 static void mtd_release(struct device *dev)
94 dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
96 /* remove /dev/mtdXro node if needed */
98 device_destroy(&mtd_class, index + 1);
101 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
103 struct mtd_info *mtd = dev_to_mtd(dev);
105 if (mtd && mtd->suspend)
106 return mtd->suspend(mtd);
111 static int mtd_cls_resume(struct device *dev)
113 struct mtd_info *mtd = dev_to_mtd(dev);
115 if (mtd && mtd->resume)
120 static ssize_t mtd_type_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct mtd_info *mtd = dev_to_mtd(dev);
152 return snprintf(buf, PAGE_SIZE, "%s\n", type);
154 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
156 static ssize_t mtd_flags_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
159 struct mtd_info *mtd = dev_to_mtd(dev);
161 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
164 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
166 static ssize_t mtd_size_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 struct mtd_info *mtd = dev_to_mtd(dev);
171 return snprintf(buf, PAGE_SIZE, "%llu\n",
172 (unsigned long long)mtd->size);
175 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
177 static ssize_t mtd_erasesize_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct mtd_info *mtd = dev_to_mtd(dev);
182 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
185 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
187 static ssize_t mtd_writesize_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
190 struct mtd_info *mtd = dev_to_mtd(dev);
192 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
195 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
197 static ssize_t mtd_subpagesize_show(struct device *dev,
198 struct device_attribute *attr, char *buf)
200 struct mtd_info *mtd = dev_to_mtd(dev);
201 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
203 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
206 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
208 static ssize_t mtd_oobsize_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct mtd_info *mtd = dev_to_mtd(dev);
213 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
216 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
218 static ssize_t mtd_numeraseregions_show(struct device *dev,
219 struct device_attribute *attr, char *buf)
221 struct mtd_info *mtd = dev_to_mtd(dev);
223 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
226 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
229 static ssize_t mtd_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct mtd_info *mtd = dev_to_mtd(dev);
234 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
237 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
239 static struct attribute *mtd_attrs[] = {
241 &dev_attr_flags.attr,
243 &dev_attr_erasesize.attr,
244 &dev_attr_writesize.attr,
245 &dev_attr_subpagesize.attr,
246 &dev_attr_oobsize.attr,
247 &dev_attr_numeraseregions.attr,
252 static struct attribute_group mtd_group = {
256 static const struct attribute_group *mtd_groups[] = {
261 static struct device_type mtd_devtype = {
263 .groups = mtd_groups,
264 .release = mtd_release,
268 * add_mtd_device - register an MTD device
269 * @mtd: pointer to new MTD device info structure
271 * Add a device to the list of MTD devices present in the system, and
272 * notify each currently active MTD 'user' of its arrival. Returns
273 * zero on success or 1 on failure, which currently will only happen
274 * if there is insufficient memory or a sysfs error.
277 int add_mtd_device(struct mtd_info *mtd)
279 struct mtd_notifier *not;
282 if (!mtd->backing_dev_info) {
285 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
288 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
291 mtd->backing_dev_info = &mtd_bdi_unmappable;
296 BUG_ON(mtd->writesize == 0);
297 mutex_lock(&mtd_table_mutex);
300 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
302 error = idr_get_new(&mtd_idr, mtd, &i);
303 } while (error == -EAGAIN);
311 if (is_power_of_2(mtd->erasesize))
312 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
314 mtd->erasesize_shift = 0;
316 if (is_power_of_2(mtd->writesize))
317 mtd->writesize_shift = ffs(mtd->writesize) - 1;
319 mtd->writesize_shift = 0;
321 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
322 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
324 /* Some chips always power up locked. Unlock them now */
325 if ((mtd->flags & MTD_WRITEABLE)
326 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
327 if (mtd->unlock(mtd, 0, mtd->size))
329 "%s: unlock failed, writes may not work\n",
333 /* Caller should have set dev.parent to match the
336 mtd->dev.type = &mtd_devtype;
337 mtd->dev.class = &mtd_class;
338 mtd->dev.devt = MTD_DEVT(i);
339 dev_set_name(&mtd->dev, "mtd%d", i);
340 dev_set_drvdata(&mtd->dev, mtd);
341 if (device_register(&mtd->dev) != 0)
345 device_create(&mtd_class, mtd->dev.parent,
349 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
350 /* No need to get a refcount on the module containing
351 the notifier, since we hold the mtd_table_mutex */
352 list_for_each_entry(not, &mtd_notifiers, list)
355 mutex_unlock(&mtd_table_mutex);
356 /* We _know_ we aren't being removed, because
357 our caller is still holding us here. So none
358 of this try_ nonsense, and no bitching about it
360 __module_get(THIS_MODULE);
364 idr_remove(&mtd_idr, i);
366 mutex_unlock(&mtd_table_mutex);
371 * del_mtd_device - unregister an MTD device
372 * @mtd: pointer to MTD device info structure
374 * Remove a device from the list of MTD devices present in the system,
375 * and notify each currently active MTD 'user' of its departure.
376 * Returns zero on success or 1 on failure, which currently will happen
377 * if the requested device does not appear to be present in the list.
380 int del_mtd_device (struct mtd_info *mtd)
383 struct mtd_notifier *not;
385 mutex_lock(&mtd_table_mutex);
387 if (idr_find(&mtd_idr, mtd->index) != mtd) {
392 /* No need to get a refcount on the module containing
393 the notifier, since we hold the mtd_table_mutex */
394 list_for_each_entry(not, &mtd_notifiers, list)
398 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
399 mtd->index, mtd->name, mtd->usecount);
402 device_unregister(&mtd->dev);
404 idr_remove(&mtd_idr, mtd->index);
406 module_put(THIS_MODULE);
411 mutex_unlock(&mtd_table_mutex);
416 * register_mtd_user - register a 'user' of MTD devices.
417 * @new: pointer to notifier info structure
419 * Registers a pair of callbacks function to be called upon addition
420 * or removal of MTD devices. Causes the 'add' callback to be immediately
421 * invoked for each MTD device currently present in the system.
424 void register_mtd_user (struct mtd_notifier *new)
426 struct mtd_info *mtd;
428 mutex_lock(&mtd_table_mutex);
430 list_add(&new->list, &mtd_notifiers);
432 __module_get(THIS_MODULE);
434 mtd_for_each_device(mtd)
437 mutex_unlock(&mtd_table_mutex);
441 * unregister_mtd_user - unregister a 'user' of MTD devices.
442 * @old: pointer to notifier info structure
444 * Removes a callback function pair from the list of 'users' to be
445 * notified upon addition or removal of MTD devices. Causes the
446 * 'remove' callback to be immediately invoked for each MTD device
447 * currently present in the system.
450 int unregister_mtd_user (struct mtd_notifier *old)
452 struct mtd_info *mtd;
454 mutex_lock(&mtd_table_mutex);
456 module_put(THIS_MODULE);
458 mtd_for_each_device(mtd)
461 list_del(&old->list);
462 mutex_unlock(&mtd_table_mutex);
468 * get_mtd_device - obtain a validated handle for an MTD device
469 * @mtd: last known address of the required MTD device
470 * @num: internal device number of the required MTD device
472 * Given a number and NULL address, return the num'th entry in the device
473 * table, if any. Given an address and num == -1, search the device table
474 * for a device with that address and return if it's still present. Given
475 * both, return the num'th driver only if its address matches. Return
479 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
481 struct mtd_info *ret = NULL, *other;
484 mutex_lock(&mtd_table_mutex);
487 mtd_for_each_device(other) {
493 } else if (num >= 0) {
494 ret = idr_find(&mtd_idr, num);
495 if (mtd && mtd != ret)
504 err = __get_mtd_device(ret);
508 mutex_unlock(&mtd_table_mutex);
513 int __get_mtd_device(struct mtd_info *mtd)
517 if (!try_module_get(mtd->owner))
520 if (mtd->get_device) {
522 err = mtd->get_device(mtd);
525 module_put(mtd->owner);
534 * get_mtd_device_nm - obtain a validated handle for an MTD device by
536 * @name: MTD device name to open
538 * This function returns MTD device description structure in case of
539 * success and an error code in case of failure.
542 struct mtd_info *get_mtd_device_nm(const char *name)
545 struct mtd_info *mtd = NULL, *other;
547 mutex_lock(&mtd_table_mutex);
549 mtd_for_each_device(other) {
550 if (!strcmp(name, other->name)) {
559 if (!try_module_get(mtd->owner))
562 if (mtd->get_device) {
563 err = mtd->get_device(mtd);
569 mutex_unlock(&mtd_table_mutex);
573 module_put(mtd->owner);
575 mutex_unlock(&mtd_table_mutex);
579 void put_mtd_device(struct mtd_info *mtd)
581 mutex_lock(&mtd_table_mutex);
582 __put_mtd_device(mtd);
583 mutex_unlock(&mtd_table_mutex);
587 void __put_mtd_device(struct mtd_info *mtd)
590 BUG_ON(mtd->usecount < 0);
593 mtd->put_device(mtd);
595 module_put(mtd->owner);
598 /* default_mtd_writev - default mtd writev method for MTD devices that
599 * don't implement their own
602 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
603 unsigned long count, loff_t to, size_t *retlen)
606 size_t totlen = 0, thislen;
612 for (i=0; i<count; i++) {
613 if (!vecs[i].iov_len)
615 ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
617 if (ret || thislen != vecs[i].iov_len)
619 to += vecs[i].iov_len;
627 EXPORT_SYMBOL_GPL(add_mtd_device);
628 EXPORT_SYMBOL_GPL(del_mtd_device);
629 EXPORT_SYMBOL_GPL(get_mtd_device);
630 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
631 EXPORT_SYMBOL_GPL(__get_mtd_device);
632 EXPORT_SYMBOL_GPL(put_mtd_device);
633 EXPORT_SYMBOL_GPL(__put_mtd_device);
634 EXPORT_SYMBOL_GPL(register_mtd_user);
635 EXPORT_SYMBOL_GPL(unregister_mtd_user);
636 EXPORT_SYMBOL_GPL(default_mtd_writev);
638 #ifdef CONFIG_PROC_FS
640 /*====================================================================*/
641 /* Support for /proc/mtd */
643 static struct proc_dir_entry *proc_mtd;
645 static inline int mtd_proc_info(char *buf, struct mtd_info *this)
647 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
648 (unsigned long long)this->size,
649 this->erasesize, this->name);
652 static int mtd_read_proc (char *page, char **start, off_t off, int count,
653 int *eof, void *data_unused)
655 struct mtd_info *mtd;
659 mutex_lock(&mtd_table_mutex);
661 len = sprintf(page, "dev: size erasesize name\n");
662 mtd_for_each_device(mtd) {
663 l = mtd_proc_info(page + len, mtd);
665 if (len+begin > off+count)
667 if (len+begin < off) {
676 mutex_unlock(&mtd_table_mutex);
677 if (off >= len+begin)
679 *start = page + (off-begin);
680 return ((count < begin+len-off) ? count : begin+len-off);
683 #endif /* CONFIG_PROC_FS */
685 /*====================================================================*/
688 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
694 ret = bdi_register(bdi, NULL, name);
702 static int __init init_mtd(void)
706 ret = class_register(&mtd_class);
710 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
714 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
718 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
722 #ifdef CONFIG_PROC_FS
723 if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
724 proc_mtd->read_proc = mtd_read_proc;
725 #endif /* CONFIG_PROC_FS */
729 bdi_destroy(&mtd_bdi_ro_mappable);
731 bdi_destroy(&mtd_bdi_unmappable);
733 class_unregister(&mtd_class);
735 pr_err("Error registering mtd class or bdi: %d\n", ret);
739 static void __exit cleanup_mtd(void)
741 #ifdef CONFIG_PROC_FS
743 remove_proc_entry( "mtd", NULL);
744 #endif /* CONFIG_PROC_FS */
745 class_unregister(&mtd_class);
746 bdi_destroy(&mtd_bdi_unmappable);
747 bdi_destroy(&mtd_bdi_ro_mappable);
748 bdi_destroy(&mtd_bdi_rw_mappable);
751 module_init(init_mtd);
752 module_exit(cleanup_mtd);
754 MODULE_LICENSE("GPL");
755 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
756 MODULE_DESCRIPTION("Core MTD registration and access routines");