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/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/major.h>
32 #include <linux/err.h>
33 #include <linux/ioctl.h>
34 #include <linux/init.h>
35 #include <linux/proc_fs.h>
36 #include <linux/idr.h>
37 #include <linux/backing-dev.h>
38 #include <linux/gfp.h>
40 #include <linux/mtd/mtd.h>
44 * backing device capabilities for non-mappable devices (such as NAND flash)
45 * - permits private mappings, copies are taken of the data
47 static struct backing_dev_info mtd_bdi_unmappable
= {
48 .capabilities
= BDI_CAP_MAP_COPY
,
52 * backing device capabilities for R/O mappable devices (such as ROM)
53 * - permits private mappings, copies are taken of the data
54 * - permits non-writable shared mappings
56 static struct backing_dev_info mtd_bdi_ro_mappable
= {
57 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
58 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
),
62 * backing device capabilities for writable mappable devices (such as RAM)
63 * - permits private mappings, copies are taken of the data
64 * - permits non-writable shared mappings
66 static struct backing_dev_info mtd_bdi_rw_mappable
= {
67 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
68 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
|
72 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
);
73 static int mtd_cls_resume(struct device
*dev
);
75 static struct class mtd_class
= {
78 .suspend
= mtd_cls_suspend
,
79 .resume
= mtd_cls_resume
,
82 static DEFINE_IDR(mtd_idr
);
84 /* These are exported solely for the purpose of mtd_blkdevs.c. You
85 should not use them for _anything_ else */
86 DEFINE_MUTEX(mtd_table_mutex
);
87 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
89 struct mtd_info
*__mtd_next_device(int i
)
91 return idr_get_next(&mtd_idr
, &i
);
93 EXPORT_SYMBOL_GPL(__mtd_next_device
);
95 static LIST_HEAD(mtd_notifiers
);
98 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
99 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
101 #define MTD_DEVT(index) 0
104 /* REVISIT once MTD uses the driver model better, whoever allocates
105 * the mtd_info will probably want to use the release() hook...
107 static void mtd_release(struct device
*dev
)
109 dev_t index
= MTD_DEVT(dev_to_mtd(dev
)->index
);
111 /* remove /dev/mtdXro node if needed */
113 device_destroy(&mtd_class
, index
+ 1);
116 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
)
118 struct mtd_info
*mtd
= dev_to_mtd(dev
);
120 if (mtd
&& mtd
->suspend
)
121 return mtd
->suspend(mtd
);
126 static int mtd_cls_resume(struct device
*dev
)
128 struct mtd_info
*mtd
= dev_to_mtd(dev
);
130 if (mtd
&& mtd
->resume
)
135 static ssize_t
mtd_type_show(struct device
*dev
,
136 struct device_attribute
*attr
, char *buf
)
138 struct mtd_info
*mtd
= dev_to_mtd(dev
);
167 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
169 static DEVICE_ATTR(type
, S_IRUGO
, mtd_type_show
, NULL
);
171 static ssize_t
mtd_flags_show(struct device
*dev
,
172 struct device_attribute
*attr
, char *buf
)
174 struct mtd_info
*mtd
= dev_to_mtd(dev
);
176 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n", (unsigned long)mtd
->flags
);
179 static DEVICE_ATTR(flags
, S_IRUGO
, mtd_flags_show
, NULL
);
181 static ssize_t
mtd_size_show(struct device
*dev
,
182 struct device_attribute
*attr
, char *buf
)
184 struct mtd_info
*mtd
= dev_to_mtd(dev
);
186 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
187 (unsigned long long)mtd
->size
);
190 static DEVICE_ATTR(size
, S_IRUGO
, mtd_size_show
, NULL
);
192 static ssize_t
mtd_erasesize_show(struct device
*dev
,
193 struct device_attribute
*attr
, char *buf
)
195 struct mtd_info
*mtd
= dev_to_mtd(dev
);
197 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->erasesize
);
200 static DEVICE_ATTR(erasesize
, S_IRUGO
, mtd_erasesize_show
, NULL
);
202 static ssize_t
mtd_writesize_show(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
205 struct mtd_info
*mtd
= dev_to_mtd(dev
);
207 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->writesize
);
210 static DEVICE_ATTR(writesize
, S_IRUGO
, mtd_writesize_show
, NULL
);
212 static ssize_t
mtd_subpagesize_show(struct device
*dev
,
213 struct device_attribute
*attr
, char *buf
)
215 struct mtd_info
*mtd
= dev_to_mtd(dev
);
216 unsigned int subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
218 return snprintf(buf
, PAGE_SIZE
, "%u\n", subpagesize
);
221 static DEVICE_ATTR(subpagesize
, S_IRUGO
, mtd_subpagesize_show
, NULL
);
223 static ssize_t
mtd_oobsize_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct mtd_info
*mtd
= dev_to_mtd(dev
);
228 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->oobsize
);
231 static DEVICE_ATTR(oobsize
, S_IRUGO
, mtd_oobsize_show
, NULL
);
233 static ssize_t
mtd_numeraseregions_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 struct mtd_info
*mtd
= dev_to_mtd(dev
);
238 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->numeraseregions
);
241 static DEVICE_ATTR(numeraseregions
, S_IRUGO
, mtd_numeraseregions_show
,
244 static ssize_t
mtd_name_show(struct device
*dev
,
245 struct device_attribute
*attr
, char *buf
)
247 struct mtd_info
*mtd
= dev_to_mtd(dev
);
249 return snprintf(buf
, PAGE_SIZE
, "%s\n", mtd
->name
);
252 static DEVICE_ATTR(name
, S_IRUGO
, mtd_name_show
, NULL
);
254 static struct attribute
*mtd_attrs
[] = {
256 &dev_attr_flags
.attr
,
258 &dev_attr_erasesize
.attr
,
259 &dev_attr_writesize
.attr
,
260 &dev_attr_subpagesize
.attr
,
261 &dev_attr_oobsize
.attr
,
262 &dev_attr_numeraseregions
.attr
,
267 static struct attribute_group mtd_group
= {
271 static const struct attribute_group
*mtd_groups
[] = {
276 static struct device_type mtd_devtype
= {
278 .groups
= mtd_groups
,
279 .release
= mtd_release
,
283 * add_mtd_device - register an MTD device
284 * @mtd: pointer to new MTD device info structure
286 * Add a device to the list of MTD devices present in the system, and
287 * notify each currently active MTD 'user' of its arrival. Returns
288 * zero on success or 1 on failure, which currently will only happen
289 * if there is insufficient memory or a sysfs error.
292 int add_mtd_device(struct mtd_info
*mtd
)
294 struct mtd_notifier
*not;
297 if (!mtd
->backing_dev_info
) {
300 mtd
->backing_dev_info
= &mtd_bdi_rw_mappable
;
303 mtd
->backing_dev_info
= &mtd_bdi_ro_mappable
;
306 mtd
->backing_dev_info
= &mtd_bdi_unmappable
;
311 BUG_ON(mtd
->writesize
== 0);
312 mutex_lock(&mtd_table_mutex
);
315 if (!idr_pre_get(&mtd_idr
, GFP_KERNEL
))
317 error
= idr_get_new(&mtd_idr
, mtd
, &i
);
318 } while (error
== -EAGAIN
);
326 if (is_power_of_2(mtd
->erasesize
))
327 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
329 mtd
->erasesize_shift
= 0;
331 if (is_power_of_2(mtd
->writesize
))
332 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
334 mtd
->writesize_shift
= 0;
336 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
337 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
339 /* Some chips always power up locked. Unlock them now */
340 if ((mtd
->flags
& MTD_WRITEABLE
)
341 && (mtd
->flags
& MTD_POWERUP_LOCK
) && mtd
->unlock
) {
342 if (mtd
->unlock(mtd
, 0, mtd
->size
))
344 "%s: unlock failed, writes may not work\n",
348 /* Caller should have set dev.parent to match the
351 mtd
->dev
.type
= &mtd_devtype
;
352 mtd
->dev
.class = &mtd_class
;
353 mtd
->dev
.devt
= MTD_DEVT(i
);
354 dev_set_name(&mtd
->dev
, "mtd%d", i
);
355 dev_set_drvdata(&mtd
->dev
, mtd
);
356 if (device_register(&mtd
->dev
) != 0)
360 device_create(&mtd_class
, mtd
->dev
.parent
,
364 DEBUG(0, "mtd: Giving out device %d to %s\n", i
, mtd
->name
);
365 /* No need to get a refcount on the module containing
366 the notifier, since we hold the mtd_table_mutex */
367 list_for_each_entry(not, &mtd_notifiers
, list
)
370 mutex_unlock(&mtd_table_mutex
);
371 /* We _know_ we aren't being removed, because
372 our caller is still holding us here. So none
373 of this try_ nonsense, and no bitching about it
375 __module_get(THIS_MODULE
);
379 idr_remove(&mtd_idr
, i
);
381 mutex_unlock(&mtd_table_mutex
);
386 * del_mtd_device - unregister an MTD device
387 * @mtd: pointer to MTD device info structure
389 * Remove a device from the list of MTD devices present in the system,
390 * and notify each currently active MTD 'user' of its departure.
391 * Returns zero on success or 1 on failure, which currently will happen
392 * if the requested device does not appear to be present in the list.
395 int del_mtd_device (struct mtd_info
*mtd
)
398 struct mtd_notifier
*not;
400 mutex_lock(&mtd_table_mutex
);
402 if (idr_find(&mtd_idr
, mtd
->index
) != mtd
) {
407 /* No need to get a refcount on the module containing
408 the notifier, since we hold the mtd_table_mutex */
409 list_for_each_entry(not, &mtd_notifiers
, list
)
413 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
414 mtd
->index
, mtd
->name
, mtd
->usecount
);
417 device_unregister(&mtd
->dev
);
419 idr_remove(&mtd_idr
, mtd
->index
);
421 module_put(THIS_MODULE
);
426 mutex_unlock(&mtd_table_mutex
);
431 * register_mtd_user - register a 'user' of MTD devices.
432 * @new: pointer to notifier info structure
434 * Registers a pair of callbacks function to be called upon addition
435 * or removal of MTD devices. Causes the 'add' callback to be immediately
436 * invoked for each MTD device currently present in the system.
439 void register_mtd_user (struct mtd_notifier
*new)
441 struct mtd_info
*mtd
;
443 mutex_lock(&mtd_table_mutex
);
445 list_add(&new->list
, &mtd_notifiers
);
447 __module_get(THIS_MODULE
);
449 mtd_for_each_device(mtd
)
452 mutex_unlock(&mtd_table_mutex
);
456 * unregister_mtd_user - unregister a 'user' of MTD devices.
457 * @old: pointer to notifier info structure
459 * Removes a callback function pair from the list of 'users' to be
460 * notified upon addition or removal of MTD devices. Causes the
461 * 'remove' callback to be immediately invoked for each MTD device
462 * currently present in the system.
465 int unregister_mtd_user (struct mtd_notifier
*old
)
467 struct mtd_info
*mtd
;
469 mutex_lock(&mtd_table_mutex
);
471 module_put(THIS_MODULE
);
473 mtd_for_each_device(mtd
)
476 list_del(&old
->list
);
477 mutex_unlock(&mtd_table_mutex
);
483 * get_mtd_device - obtain a validated handle for an MTD device
484 * @mtd: last known address of the required MTD device
485 * @num: internal device number of the required MTD device
487 * Given a number and NULL address, return the num'th entry in the device
488 * table, if any. Given an address and num == -1, search the device table
489 * for a device with that address and return if it's still present. Given
490 * both, return the num'th driver only if its address matches. Return
494 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
496 struct mtd_info
*ret
= NULL
, *other
;
499 mutex_lock(&mtd_table_mutex
);
502 mtd_for_each_device(other
) {
508 } else if (num
>= 0) {
509 ret
= idr_find(&mtd_idr
, num
);
510 if (mtd
&& mtd
!= ret
)
519 err
= __get_mtd_device(ret
);
523 mutex_unlock(&mtd_table_mutex
);
528 int __get_mtd_device(struct mtd_info
*mtd
)
532 if (!try_module_get(mtd
->owner
))
535 if (mtd
->get_device
) {
537 err
= mtd
->get_device(mtd
);
540 module_put(mtd
->owner
);
549 * get_mtd_device_nm - obtain a validated handle for an MTD device by
551 * @name: MTD device name to open
553 * This function returns MTD device description structure in case of
554 * success and an error code in case of failure.
557 struct mtd_info
*get_mtd_device_nm(const char *name
)
560 struct mtd_info
*mtd
= NULL
, *other
;
562 mutex_lock(&mtd_table_mutex
);
564 mtd_for_each_device(other
) {
565 if (!strcmp(name
, other
->name
)) {
574 if (!try_module_get(mtd
->owner
))
577 if (mtd
->get_device
) {
578 err
= mtd
->get_device(mtd
);
584 mutex_unlock(&mtd_table_mutex
);
588 module_put(mtd
->owner
);
590 mutex_unlock(&mtd_table_mutex
);
594 void put_mtd_device(struct mtd_info
*mtd
)
596 mutex_lock(&mtd_table_mutex
);
597 __put_mtd_device(mtd
);
598 mutex_unlock(&mtd_table_mutex
);
602 void __put_mtd_device(struct mtd_info
*mtd
)
605 BUG_ON(mtd
->usecount
< 0);
608 mtd
->put_device(mtd
);
610 module_put(mtd
->owner
);
613 /* default_mtd_writev - default mtd writev method for MTD devices that
614 * don't implement their own
617 int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
618 unsigned long count
, loff_t to
, size_t *retlen
)
621 size_t totlen
= 0, thislen
;
627 for (i
=0; i
<count
; i
++) {
628 if (!vecs
[i
].iov_len
)
630 ret
= mtd
->write(mtd
, to
, vecs
[i
].iov_len
, &thislen
, vecs
[i
].iov_base
);
632 if (ret
|| thislen
!= vecs
[i
].iov_len
)
634 to
+= vecs
[i
].iov_len
;
643 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
644 * @size: A pointer to the ideal or maximum size of the allocation. Points
645 * to the actual allocation size on success.
647 * This routine attempts to allocate a contiguous kernel buffer up to
648 * the specified size, backing off the size of the request exponentially
649 * until the request succeeds or until the allocation size falls below
650 * the system page size. This attempts to make sure it does not adversely
651 * impact system performance, so when allocating more than one page, we
652 * ask the memory allocator to avoid re-trying, swapping, writing back
655 * Note, this function also makes sure that the allocated buffer is aligned to
656 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
658 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
659 * to handle smaller (i.e. degraded) buffer allocations under low- or
660 * fragmented-memory situations where such reduced allocations, from a
661 * requested ideal, are allowed.
663 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
665 void *mtd_kmalloc_up_to(const struct mtd_info
*mtd
, size_t *size
)
667 gfp_t flags
= __GFP_NOWARN
| __GFP_WAIT
|
668 __GFP_NORETRY
| __GFP_NO_KSWAPD
;
669 size_t min_alloc
= max_t(size_t, mtd
->writesize
, PAGE_SIZE
);
672 *size
= min_t(size_t, *size
, KMALLOC_MAX_SIZE
);
674 while (*size
> min_alloc
) {
675 kbuf
= kmalloc(*size
, flags
);
680 *size
= ALIGN(*size
, mtd
->writesize
);
684 * For the last resort allocation allow 'kmalloc()' to do all sorts of
685 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
687 return kmalloc(*size
, GFP_KERNEL
);
690 EXPORT_SYMBOL_GPL(add_mtd_device
);
691 EXPORT_SYMBOL_GPL(del_mtd_device
);
692 EXPORT_SYMBOL_GPL(get_mtd_device
);
693 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
694 EXPORT_SYMBOL_GPL(__get_mtd_device
);
695 EXPORT_SYMBOL_GPL(put_mtd_device
);
696 EXPORT_SYMBOL_GPL(__put_mtd_device
);
697 EXPORT_SYMBOL_GPL(register_mtd_user
);
698 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
699 EXPORT_SYMBOL_GPL(default_mtd_writev
);
700 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to
);
702 #ifdef CONFIG_PROC_FS
704 /*====================================================================*/
705 /* Support for /proc/mtd */
707 static struct proc_dir_entry
*proc_mtd
;
709 static int mtd_proc_show(struct seq_file
*m
, void *v
)
711 struct mtd_info
*mtd
;
713 seq_puts(m
, "dev: size erasesize name\n");
714 mutex_lock(&mtd_table_mutex
);
715 mtd_for_each_device(mtd
) {
716 seq_printf(m
, "mtd%d: %8.8llx %8.8x \"%s\"\n",
717 mtd
->index
, (unsigned long long)mtd
->size
,
718 mtd
->erasesize
, mtd
->name
);
720 mutex_unlock(&mtd_table_mutex
);
724 static int mtd_proc_open(struct inode
*inode
, struct file
*file
)
726 return single_open(file
, mtd_proc_show
, NULL
);
729 static const struct file_operations mtd_proc_ops
= {
730 .open
= mtd_proc_open
,
733 .release
= single_release
,
735 #endif /* CONFIG_PROC_FS */
737 /*====================================================================*/
740 static int __init
mtd_bdi_init(struct backing_dev_info
*bdi
, const char *name
)
746 ret
= bdi_register(bdi
, NULL
, name
);
754 static int __init
init_mtd(void)
758 ret
= class_register(&mtd_class
);
762 ret
= mtd_bdi_init(&mtd_bdi_unmappable
, "mtd-unmap");
766 ret
= mtd_bdi_init(&mtd_bdi_ro_mappable
, "mtd-romap");
770 ret
= mtd_bdi_init(&mtd_bdi_rw_mappable
, "mtd-rwmap");
774 #ifdef CONFIG_PROC_FS
775 proc_mtd
= proc_create("mtd", 0, NULL
, &mtd_proc_ops
);
776 #endif /* CONFIG_PROC_FS */
780 bdi_destroy(&mtd_bdi_ro_mappable
);
782 bdi_destroy(&mtd_bdi_unmappable
);
784 class_unregister(&mtd_class
);
786 pr_err("Error registering mtd class or bdi: %d\n", ret
);
790 static void __exit
cleanup_mtd(void)
792 #ifdef CONFIG_PROC_FS
794 remove_proc_entry( "mtd", NULL
);
795 #endif /* CONFIG_PROC_FS */
796 class_unregister(&mtd_class
);
797 bdi_destroy(&mtd_bdi_unmappable
);
798 bdi_destroy(&mtd_bdi_ro_mappable
);
799 bdi_destroy(&mtd_bdi_rw_mappable
);
802 module_init(init_mtd
);
803 module_exit(cleanup_mtd
);
805 MODULE_LICENSE("GPL");
806 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
807 MODULE_DESCRIPTION("Core MTD registration and access routines");