2 * Core registration and callback routines for MTD
5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2006 Red Hat UK Limited
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/ptrace.h>
27 #include <linux/string.h>
28 #include <linux/timer.h>
29 #include <linux/major.h>
31 #include <linux/err.h>
32 #include <linux/ioctl.h>
33 #include <linux/init.h>
34 #include <linux/proc_fs.h>
35 #include <linux/idr.h>
36 #include <linux/backing-dev.h>
37 #include <linux/gfp.h>
39 #include <linux/mtd/mtd.h>
43 * backing device capabilities for non-mappable devices (such as NAND flash)
44 * - permits private mappings, copies are taken of the data
46 static struct backing_dev_info mtd_bdi_unmappable
= {
47 .capabilities
= BDI_CAP_MAP_COPY
,
51 * backing device capabilities for R/O mappable devices (such as ROM)
52 * - permits private mappings, copies are taken of the data
53 * - permits non-writable shared mappings
55 static struct backing_dev_info mtd_bdi_ro_mappable
= {
56 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
57 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
),
61 * backing device capabilities for writable mappable devices (such as RAM)
62 * - permits private mappings, copies are taken of the data
63 * - permits non-writable shared mappings
65 static struct backing_dev_info mtd_bdi_rw_mappable
= {
66 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
67 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
|
71 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
);
72 static int mtd_cls_resume(struct device
*dev
);
74 static struct class mtd_class
= {
77 .suspend
= mtd_cls_suspend
,
78 .resume
= mtd_cls_resume
,
81 static DEFINE_IDR(mtd_idr
);
83 /* These are exported solely for the purpose of mtd_blkdevs.c. You
84 should not use them for _anything_ else */
85 DEFINE_MUTEX(mtd_table_mutex
);
86 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
88 struct mtd_info
*__mtd_next_device(int i
)
90 return idr_get_next(&mtd_idr
, &i
);
92 EXPORT_SYMBOL_GPL(__mtd_next_device
);
94 static LIST_HEAD(mtd_notifiers
);
97 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
98 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
100 #define MTD_DEVT(index) 0
103 /* REVISIT once MTD uses the driver model better, whoever allocates
104 * the mtd_info will probably want to use the release() hook...
106 static void mtd_release(struct device
*dev
)
108 dev_t index
= MTD_DEVT(dev_to_mtd(dev
)->index
);
110 /* remove /dev/mtdXro node if needed */
112 device_destroy(&mtd_class
, index
+ 1);
115 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
)
117 struct mtd_info
*mtd
= dev_to_mtd(dev
);
119 if (mtd
&& mtd
->suspend
)
120 return mtd
->suspend(mtd
);
125 static int mtd_cls_resume(struct device
*dev
)
127 struct mtd_info
*mtd
= dev_to_mtd(dev
);
129 if (mtd
&& mtd
->resume
)
134 static ssize_t
mtd_type_show(struct device
*dev
,
135 struct device_attribute
*attr
, char *buf
)
137 struct mtd_info
*mtd
= dev_to_mtd(dev
);
166 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
168 static DEVICE_ATTR(type
, S_IRUGO
, mtd_type_show
, NULL
);
170 static ssize_t
mtd_flags_show(struct device
*dev
,
171 struct device_attribute
*attr
, char *buf
)
173 struct mtd_info
*mtd
= dev_to_mtd(dev
);
175 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n", (unsigned long)mtd
->flags
);
178 static DEVICE_ATTR(flags
, S_IRUGO
, mtd_flags_show
, NULL
);
180 static ssize_t
mtd_size_show(struct device
*dev
,
181 struct device_attribute
*attr
, char *buf
)
183 struct mtd_info
*mtd
= dev_to_mtd(dev
);
185 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
186 (unsigned long long)mtd
->size
);
189 static DEVICE_ATTR(size
, S_IRUGO
, mtd_size_show
, NULL
);
191 static ssize_t
mtd_erasesize_show(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
194 struct mtd_info
*mtd
= dev_to_mtd(dev
);
196 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->erasesize
);
199 static DEVICE_ATTR(erasesize
, S_IRUGO
, mtd_erasesize_show
, NULL
);
201 static ssize_t
mtd_writesize_show(struct device
*dev
,
202 struct device_attribute
*attr
, char *buf
)
204 struct mtd_info
*mtd
= dev_to_mtd(dev
);
206 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->writesize
);
209 static DEVICE_ATTR(writesize
, S_IRUGO
, mtd_writesize_show
, NULL
);
211 static ssize_t
mtd_subpagesize_show(struct device
*dev
,
212 struct device_attribute
*attr
, char *buf
)
214 struct mtd_info
*mtd
= dev_to_mtd(dev
);
215 unsigned int subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
217 return snprintf(buf
, PAGE_SIZE
, "%u\n", subpagesize
);
220 static DEVICE_ATTR(subpagesize
, S_IRUGO
, mtd_subpagesize_show
, NULL
);
222 static ssize_t
mtd_oobsize_show(struct device
*dev
,
223 struct device_attribute
*attr
, char *buf
)
225 struct mtd_info
*mtd
= dev_to_mtd(dev
);
227 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->oobsize
);
230 static DEVICE_ATTR(oobsize
, S_IRUGO
, mtd_oobsize_show
, NULL
);
232 static ssize_t
mtd_numeraseregions_show(struct device
*dev
,
233 struct device_attribute
*attr
, char *buf
)
235 struct mtd_info
*mtd
= dev_to_mtd(dev
);
237 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->numeraseregions
);
240 static DEVICE_ATTR(numeraseregions
, S_IRUGO
, mtd_numeraseregions_show
,
243 static ssize_t
mtd_name_show(struct device
*dev
,
244 struct device_attribute
*attr
, char *buf
)
246 struct mtd_info
*mtd
= dev_to_mtd(dev
);
248 return snprintf(buf
, PAGE_SIZE
, "%s\n", mtd
->name
);
251 static DEVICE_ATTR(name
, S_IRUGO
, mtd_name_show
, NULL
);
253 static struct attribute
*mtd_attrs
[] = {
255 &dev_attr_flags
.attr
,
257 &dev_attr_erasesize
.attr
,
258 &dev_attr_writesize
.attr
,
259 &dev_attr_subpagesize
.attr
,
260 &dev_attr_oobsize
.attr
,
261 &dev_attr_numeraseregions
.attr
,
266 static struct attribute_group mtd_group
= {
270 static const struct attribute_group
*mtd_groups
[] = {
275 static struct device_type mtd_devtype
= {
277 .groups
= mtd_groups
,
278 .release
= mtd_release
,
282 * add_mtd_device - register an MTD device
283 * @mtd: pointer to new MTD device info structure
285 * Add a device to the list of MTD devices present in the system, and
286 * notify each currently active MTD 'user' of its arrival. Returns
287 * zero on success or 1 on failure, which currently will only happen
288 * if there is insufficient memory or a sysfs error.
291 int add_mtd_device(struct mtd_info
*mtd
)
293 struct mtd_notifier
*not;
296 if (!mtd
->backing_dev_info
) {
299 mtd
->backing_dev_info
= &mtd_bdi_rw_mappable
;
302 mtd
->backing_dev_info
= &mtd_bdi_ro_mappable
;
305 mtd
->backing_dev_info
= &mtd_bdi_unmappable
;
310 BUG_ON(mtd
->writesize
== 0);
311 mutex_lock(&mtd_table_mutex
);
314 if (!idr_pre_get(&mtd_idr
, GFP_KERNEL
))
316 error
= idr_get_new(&mtd_idr
, mtd
, &i
);
317 } while (error
== -EAGAIN
);
325 if (is_power_of_2(mtd
->erasesize
))
326 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
328 mtd
->erasesize_shift
= 0;
330 if (is_power_of_2(mtd
->writesize
))
331 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
333 mtd
->writesize_shift
= 0;
335 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
336 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
338 /* Some chips always power up locked. Unlock them now */
339 if ((mtd
->flags
& MTD_WRITEABLE
)
340 && (mtd
->flags
& MTD_POWERUP_LOCK
) && mtd
->unlock
) {
341 if (mtd
->unlock(mtd
, 0, mtd
->size
))
343 "%s: unlock failed, writes may not work\n",
347 /* Caller should have set dev.parent to match the
350 mtd
->dev
.type
= &mtd_devtype
;
351 mtd
->dev
.class = &mtd_class
;
352 mtd
->dev
.devt
= MTD_DEVT(i
);
353 dev_set_name(&mtd
->dev
, "mtd%d", i
);
354 dev_set_drvdata(&mtd
->dev
, mtd
);
355 if (device_register(&mtd
->dev
) != 0)
359 device_create(&mtd_class
, mtd
->dev
.parent
,
363 DEBUG(0, "mtd: Giving out device %d to %s\n", i
, mtd
->name
);
364 /* No need to get a refcount on the module containing
365 the notifier, since we hold the mtd_table_mutex */
366 list_for_each_entry(not, &mtd_notifiers
, list
)
369 mutex_unlock(&mtd_table_mutex
);
370 /* We _know_ we aren't being removed, because
371 our caller is still holding us here. So none
372 of this try_ nonsense, and no bitching about it
374 __module_get(THIS_MODULE
);
378 idr_remove(&mtd_idr
, i
);
380 mutex_unlock(&mtd_table_mutex
);
385 * del_mtd_device - unregister an MTD device
386 * @mtd: pointer to MTD device info structure
388 * Remove a device from the list of MTD devices present in the system,
389 * and notify each currently active MTD 'user' of its departure.
390 * Returns zero on success or 1 on failure, which currently will happen
391 * if the requested device does not appear to be present in the list.
394 int del_mtd_device (struct mtd_info
*mtd
)
397 struct mtd_notifier
*not;
399 mutex_lock(&mtd_table_mutex
);
401 if (idr_find(&mtd_idr
, mtd
->index
) != mtd
) {
406 /* No need to get a refcount on the module containing
407 the notifier, since we hold the mtd_table_mutex */
408 list_for_each_entry(not, &mtd_notifiers
, list
)
412 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
413 mtd
->index
, mtd
->name
, mtd
->usecount
);
416 device_unregister(&mtd
->dev
);
418 idr_remove(&mtd_idr
, mtd
->index
);
420 module_put(THIS_MODULE
);
425 mutex_unlock(&mtd_table_mutex
);
430 * register_mtd_user - register a 'user' of MTD devices.
431 * @new: pointer to notifier info structure
433 * Registers a pair of callbacks function to be called upon addition
434 * or removal of MTD devices. Causes the 'add' callback to be immediately
435 * invoked for each MTD device currently present in the system.
438 void register_mtd_user (struct mtd_notifier
*new)
440 struct mtd_info
*mtd
;
442 mutex_lock(&mtd_table_mutex
);
444 list_add(&new->list
, &mtd_notifiers
);
446 __module_get(THIS_MODULE
);
448 mtd_for_each_device(mtd
)
451 mutex_unlock(&mtd_table_mutex
);
455 * unregister_mtd_user - unregister a 'user' of MTD devices.
456 * @old: pointer to notifier info structure
458 * Removes a callback function pair from the list of 'users' to be
459 * notified upon addition or removal of MTD devices. Causes the
460 * 'remove' callback to be immediately invoked for each MTD device
461 * currently present in the system.
464 int unregister_mtd_user (struct mtd_notifier
*old
)
466 struct mtd_info
*mtd
;
468 mutex_lock(&mtd_table_mutex
);
470 module_put(THIS_MODULE
);
472 mtd_for_each_device(mtd
)
475 list_del(&old
->list
);
476 mutex_unlock(&mtd_table_mutex
);
482 * get_mtd_device - obtain a validated handle for an MTD device
483 * @mtd: last known address of the required MTD device
484 * @num: internal device number of the required MTD device
486 * Given a number and NULL address, return the num'th entry in the device
487 * table, if any. Given an address and num == -1, search the device table
488 * for a device with that address and return if it's still present. Given
489 * both, return the num'th driver only if its address matches. Return
493 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
495 struct mtd_info
*ret
= NULL
, *other
;
498 mutex_lock(&mtd_table_mutex
);
501 mtd_for_each_device(other
) {
507 } else if (num
>= 0) {
508 ret
= idr_find(&mtd_idr
, num
);
509 if (mtd
&& mtd
!= ret
)
518 err
= __get_mtd_device(ret
);
522 mutex_unlock(&mtd_table_mutex
);
527 int __get_mtd_device(struct mtd_info
*mtd
)
531 if (!try_module_get(mtd
->owner
))
534 if (mtd
->get_device
) {
536 err
= mtd
->get_device(mtd
);
539 module_put(mtd
->owner
);
548 * get_mtd_device_nm - obtain a validated handle for an MTD device by
550 * @name: MTD device name to open
552 * This function returns MTD device description structure in case of
553 * success and an error code in case of failure.
556 struct mtd_info
*get_mtd_device_nm(const char *name
)
559 struct mtd_info
*mtd
= NULL
, *other
;
561 mutex_lock(&mtd_table_mutex
);
563 mtd_for_each_device(other
) {
564 if (!strcmp(name
, other
->name
)) {
573 if (!try_module_get(mtd
->owner
))
576 if (mtd
->get_device
) {
577 err
= mtd
->get_device(mtd
);
583 mutex_unlock(&mtd_table_mutex
);
587 module_put(mtd
->owner
);
589 mutex_unlock(&mtd_table_mutex
);
593 void put_mtd_device(struct mtd_info
*mtd
)
595 mutex_lock(&mtd_table_mutex
);
596 __put_mtd_device(mtd
);
597 mutex_unlock(&mtd_table_mutex
);
601 void __put_mtd_device(struct mtd_info
*mtd
)
604 BUG_ON(mtd
->usecount
< 0);
607 mtd
->put_device(mtd
);
609 module_put(mtd
->owner
);
612 /* default_mtd_writev - default mtd writev method for MTD devices that
613 * don't implement their own
616 int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
617 unsigned long count
, loff_t to
, size_t *retlen
)
620 size_t totlen
= 0, thislen
;
626 for (i
=0; i
<count
; i
++) {
627 if (!vecs
[i
].iov_len
)
629 ret
= mtd
->write(mtd
, to
, vecs
[i
].iov_len
, &thislen
, vecs
[i
].iov_base
);
631 if (ret
|| thislen
!= vecs
[i
].iov_len
)
633 to
+= vecs
[i
].iov_len
;
641 EXPORT_SYMBOL_GPL(add_mtd_device
);
642 EXPORT_SYMBOL_GPL(del_mtd_device
);
643 EXPORT_SYMBOL_GPL(get_mtd_device
);
644 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
645 EXPORT_SYMBOL_GPL(__get_mtd_device
);
646 EXPORT_SYMBOL_GPL(put_mtd_device
);
647 EXPORT_SYMBOL_GPL(__put_mtd_device
);
648 EXPORT_SYMBOL_GPL(register_mtd_user
);
649 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
650 EXPORT_SYMBOL_GPL(default_mtd_writev
);
652 #ifdef CONFIG_PROC_FS
654 /*====================================================================*/
655 /* Support for /proc/mtd */
657 static struct proc_dir_entry
*proc_mtd
;
659 static inline int mtd_proc_info(char *buf
, struct mtd_info
*this)
661 return sprintf(buf
, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index
,
662 (unsigned long long)this->size
,
663 this->erasesize
, this->name
);
666 static int mtd_read_proc (char *page
, char **start
, off_t off
, int count
,
667 int *eof
, void *data_unused
)
669 struct mtd_info
*mtd
;
673 mutex_lock(&mtd_table_mutex
);
675 len
= sprintf(page
, "dev: size erasesize name\n");
676 mtd_for_each_device(mtd
) {
677 l
= mtd_proc_info(page
+ len
, mtd
);
679 if (len
+begin
> off
+count
)
681 if (len
+begin
< off
) {
690 mutex_unlock(&mtd_table_mutex
);
691 if (off
>= len
+begin
)
693 *start
= page
+ (off
-begin
);
694 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
697 #endif /* CONFIG_PROC_FS */
699 /*====================================================================*/
702 static int __init
mtd_bdi_init(struct backing_dev_info
*bdi
, const char *name
)
708 ret
= bdi_register(bdi
, NULL
, name
);
716 static int __init
init_mtd(void)
720 ret
= class_register(&mtd_class
);
724 ret
= mtd_bdi_init(&mtd_bdi_unmappable
, "mtd-unmap");
728 ret
= mtd_bdi_init(&mtd_bdi_ro_mappable
, "mtd-romap");
732 ret
= mtd_bdi_init(&mtd_bdi_rw_mappable
, "mtd-rwmap");
736 #ifdef CONFIG_PROC_FS
737 if ((proc_mtd
= create_proc_entry( "mtd", 0, NULL
)))
738 proc_mtd
->read_proc
= mtd_read_proc
;
739 #endif /* CONFIG_PROC_FS */
743 bdi_destroy(&mtd_bdi_ro_mappable
);
745 bdi_destroy(&mtd_bdi_unmappable
);
747 class_unregister(&mtd_class
);
749 pr_err("Error registering mtd class or bdi: %d\n", ret
);
753 static void __exit
cleanup_mtd(void)
755 #ifdef CONFIG_PROC_FS
757 remove_proc_entry( "mtd", NULL
);
758 #endif /* CONFIG_PROC_FS */
759 class_unregister(&mtd_class
);
760 bdi_destroy(&mtd_bdi_unmappable
);
761 bdi_destroy(&mtd_bdi_ro_mappable
);
762 bdi_destroy(&mtd_bdi_rw_mappable
);
765 module_init(init_mtd
);
766 module_exit(cleanup_mtd
);
768 MODULE_LICENSE("GPL");
769 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
770 MODULE_DESCRIPTION("Core MTD registration and access routines");