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>
24 #include <linux/gfp.h>
26 #include <linux/mtd/mtd.h>
30 * backing device capabilities for non-mappable devices (such as NAND flash)
31 * - permits private mappings, copies are taken of the data
33 struct backing_dev_info mtd_bdi_unmappable
= {
34 .capabilities
= BDI_CAP_MAP_COPY
,
38 * backing device capabilities for R/O mappable devices (such as ROM)
39 * - permits private mappings, copies are taken of the data
40 * - permits non-writable shared mappings
42 struct backing_dev_info mtd_bdi_ro_mappable
= {
43 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
44 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
),
48 * backing device capabilities for writable mappable devices (such as RAM)
49 * - permits private mappings, copies are taken of the data
50 * - permits non-writable shared mappings
52 struct backing_dev_info mtd_bdi_rw_mappable
= {
53 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
54 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
|
58 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
);
59 static int mtd_cls_resume(struct device
*dev
);
61 static struct class mtd_class
= {
64 .suspend
= mtd_cls_suspend
,
65 .resume
= mtd_cls_resume
,
68 static DEFINE_IDR(mtd_idr
);
70 /* These are exported solely for the purpose of mtd_blkdevs.c. You
71 should not use them for _anything_ else */
72 DEFINE_MUTEX(mtd_table_mutex
);
73 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
75 struct mtd_info
*__mtd_next_device(int i
)
77 return idr_get_next(&mtd_idr
, &i
);
79 EXPORT_SYMBOL_GPL(__mtd_next_device
);
81 static LIST_HEAD(mtd_notifiers
);
84 #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
85 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
87 #define MTD_DEVT(index) 0
90 /* REVISIT once MTD uses the driver model better, whoever allocates
91 * the mtd_info will probably want to use the release() hook...
93 static void mtd_release(struct device
*dev
)
95 dev_t index
= MTD_DEVT(dev_to_mtd(dev
)->index
);
97 /* remove /dev/mtdXro node if needed */
99 device_destroy(&mtd_class
, index
+ 1);
102 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
)
104 struct mtd_info
*mtd
= dev_to_mtd(dev
);
106 if (mtd
&& mtd
->suspend
)
107 return mtd
->suspend(mtd
);
112 static int mtd_cls_resume(struct device
*dev
)
114 struct mtd_info
*mtd
= dev_to_mtd(dev
);
116 if (mtd
&& mtd
->resume
)
121 static ssize_t
mtd_type_show(struct device
*dev
,
122 struct device_attribute
*attr
, char *buf
)
124 struct mtd_info
*mtd
= dev_to_mtd(dev
);
153 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
155 static DEVICE_ATTR(type
, S_IRUGO
, mtd_type_show
, NULL
);
157 static ssize_t
mtd_flags_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
, "0x%lx\n", (unsigned long)mtd
->flags
);
165 static DEVICE_ATTR(flags
, S_IRUGO
, mtd_flags_show
, NULL
);
167 static ssize_t
mtd_size_show(struct device
*dev
,
168 struct device_attribute
*attr
, char *buf
)
170 struct mtd_info
*mtd
= dev_to_mtd(dev
);
172 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
173 (unsigned long long)mtd
->size
);
176 static DEVICE_ATTR(size
, S_IRUGO
, mtd_size_show
, NULL
);
178 static ssize_t
mtd_erasesize_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
->erasesize
);
186 static DEVICE_ATTR(erasesize
, S_IRUGO
, mtd_erasesize_show
, NULL
);
188 static ssize_t
mtd_writesize_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
, "%lu\n", (unsigned long)mtd
->writesize
);
196 static DEVICE_ATTR(writesize
, S_IRUGO
, mtd_writesize_show
, NULL
);
198 static ssize_t
mtd_subpagesize_show(struct device
*dev
,
199 struct device_attribute
*attr
, char *buf
)
201 struct mtd_info
*mtd
= dev_to_mtd(dev
);
202 unsigned int subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
204 return snprintf(buf
, PAGE_SIZE
, "%u\n", subpagesize
);
207 static DEVICE_ATTR(subpagesize
, S_IRUGO
, mtd_subpagesize_show
, NULL
);
209 static ssize_t
mtd_oobsize_show(struct device
*dev
,
210 struct device_attribute
*attr
, char *buf
)
212 struct mtd_info
*mtd
= dev_to_mtd(dev
);
214 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->oobsize
);
217 static DEVICE_ATTR(oobsize
, S_IRUGO
, mtd_oobsize_show
, NULL
);
219 static ssize_t
mtd_numeraseregions_show(struct device
*dev
,
220 struct device_attribute
*attr
, char *buf
)
222 struct mtd_info
*mtd
= dev_to_mtd(dev
);
224 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->numeraseregions
);
227 static DEVICE_ATTR(numeraseregions
, S_IRUGO
, mtd_numeraseregions_show
,
230 static ssize_t
mtd_name_show(struct device
*dev
,
231 struct device_attribute
*attr
, char *buf
)
233 struct mtd_info
*mtd
= dev_to_mtd(dev
);
235 return snprintf(buf
, PAGE_SIZE
, "%s\n", mtd
->name
);
238 static DEVICE_ATTR(name
, S_IRUGO
, mtd_name_show
, NULL
);
240 static struct attribute
*mtd_attrs
[] = {
242 &dev_attr_flags
.attr
,
244 &dev_attr_erasesize
.attr
,
245 &dev_attr_writesize
.attr
,
246 &dev_attr_subpagesize
.attr
,
247 &dev_attr_oobsize
.attr
,
248 &dev_attr_numeraseregions
.attr
,
253 static struct attribute_group mtd_group
= {
257 static const struct attribute_group
*mtd_groups
[] = {
262 static struct device_type mtd_devtype
= {
264 .groups
= mtd_groups
,
265 .release
= mtd_release
,
269 * add_mtd_device - register an MTD device
270 * @mtd: pointer to new MTD device info structure
272 * Add a device to the list of MTD devices present in the system, and
273 * notify each currently active MTD 'user' of its arrival. Returns
274 * zero on success or 1 on failure, which currently will only happen
275 * if there is insufficient memory or a sysfs error.
278 int add_mtd_device(struct mtd_info
*mtd
)
280 struct mtd_notifier
*not;
283 if (!mtd
->backing_dev_info
) {
286 mtd
->backing_dev_info
= &mtd_bdi_rw_mappable
;
289 mtd
->backing_dev_info
= &mtd_bdi_ro_mappable
;
292 mtd
->backing_dev_info
= &mtd_bdi_unmappable
;
297 BUG_ON(mtd
->writesize
== 0);
298 mutex_lock(&mtd_table_mutex
);
301 if (!idr_pre_get(&mtd_idr
, GFP_KERNEL
))
303 error
= idr_get_new(&mtd_idr
, mtd
, &i
);
304 } while (error
== -EAGAIN
);
312 if (is_power_of_2(mtd
->erasesize
))
313 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
315 mtd
->erasesize_shift
= 0;
317 if (is_power_of_2(mtd
->writesize
))
318 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
320 mtd
->writesize_shift
= 0;
322 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
323 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
325 /* Some chips always power up locked. Unlock them now */
326 if ((mtd
->flags
& MTD_WRITEABLE
)
327 && (mtd
->flags
& MTD_POWERUP_LOCK
) && mtd
->unlock
) {
328 if (mtd
->unlock(mtd
, 0, mtd
->size
))
330 "%s: unlock failed, writes may not work\n",
334 /* Caller should have set dev.parent to match the
337 mtd
->dev
.type
= &mtd_devtype
;
338 mtd
->dev
.class = &mtd_class
;
339 mtd
->dev
.devt
= MTD_DEVT(i
);
340 dev_set_name(&mtd
->dev
, "mtd%d", i
);
341 dev_set_drvdata(&mtd
->dev
, mtd
);
342 if (device_register(&mtd
->dev
) != 0)
346 device_create(&mtd_class
, mtd
->dev
.parent
,
350 DEBUG(0, "mtd: Giving out device %d to %s\n", i
, mtd
->name
);
351 /* No need to get a refcount on the module containing
352 the notifier, since we hold the mtd_table_mutex */
353 list_for_each_entry(not, &mtd_notifiers
, list
)
356 mutex_unlock(&mtd_table_mutex
);
357 /* We _know_ we aren't being removed, because
358 our caller is still holding us here. So none
359 of this try_ nonsense, and no bitching about it
361 __module_get(THIS_MODULE
);
365 idr_remove(&mtd_idr
, i
);
367 mutex_unlock(&mtd_table_mutex
);
372 * del_mtd_device - unregister an MTD device
373 * @mtd: pointer to MTD device info structure
375 * Remove a device from the list of MTD devices present in the system,
376 * and notify each currently active MTD 'user' of its departure.
377 * Returns zero on success or 1 on failure, which currently will happen
378 * if the requested device does not appear to be present in the list.
381 int del_mtd_device (struct mtd_info
*mtd
)
384 struct mtd_notifier
*not;
386 mutex_lock(&mtd_table_mutex
);
388 if (idr_find(&mtd_idr
, mtd
->index
) != mtd
) {
393 /* No need to get a refcount on the module containing
394 the notifier, since we hold the mtd_table_mutex */
395 list_for_each_entry(not, &mtd_notifiers
, list
)
399 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
400 mtd
->index
, mtd
->name
, mtd
->usecount
);
403 device_unregister(&mtd
->dev
);
405 idr_remove(&mtd_idr
, mtd
->index
);
407 module_put(THIS_MODULE
);
412 mutex_unlock(&mtd_table_mutex
);
417 * register_mtd_user - register a 'user' of MTD devices.
418 * @new: pointer to notifier info structure
420 * Registers a pair of callbacks function to be called upon addition
421 * or removal of MTD devices. Causes the 'add' callback to be immediately
422 * invoked for each MTD device currently present in the system.
425 void register_mtd_user (struct mtd_notifier
*new)
427 struct mtd_info
*mtd
;
429 mutex_lock(&mtd_table_mutex
);
431 list_add(&new->list
, &mtd_notifiers
);
433 __module_get(THIS_MODULE
);
435 mtd_for_each_device(mtd
)
438 mutex_unlock(&mtd_table_mutex
);
442 * unregister_mtd_user - unregister a 'user' of MTD devices.
443 * @old: pointer to notifier info structure
445 * Removes a callback function pair from the list of 'users' to be
446 * notified upon addition or removal of MTD devices. Causes the
447 * 'remove' callback to be immediately invoked for each MTD device
448 * currently present in the system.
451 int unregister_mtd_user (struct mtd_notifier
*old
)
453 struct mtd_info
*mtd
;
455 mutex_lock(&mtd_table_mutex
);
457 module_put(THIS_MODULE
);
459 mtd_for_each_device(mtd
)
462 list_del(&old
->list
);
463 mutex_unlock(&mtd_table_mutex
);
469 * get_mtd_device - obtain a validated handle for an MTD device
470 * @mtd: last known address of the required MTD device
471 * @num: internal device number of the required MTD device
473 * Given a number and NULL address, return the num'th entry in the device
474 * table, if any. Given an address and num == -1, search the device table
475 * for a device with that address and return if it's still present. Given
476 * both, return the num'th driver only if its address matches. Return
480 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
482 struct mtd_info
*ret
= NULL
, *other
;
485 mutex_lock(&mtd_table_mutex
);
488 mtd_for_each_device(other
) {
494 } else if (num
>= 0) {
495 ret
= idr_find(&mtd_idr
, num
);
496 if (mtd
&& mtd
!= ret
)
505 err
= __get_mtd_device(ret
);
509 mutex_unlock(&mtd_table_mutex
);
514 int __get_mtd_device(struct mtd_info
*mtd
)
518 if (!try_module_get(mtd
->owner
))
521 if (mtd
->get_device
) {
523 err
= mtd
->get_device(mtd
);
526 module_put(mtd
->owner
);
535 * get_mtd_device_nm - obtain a validated handle for an MTD device by
537 * @name: MTD device name to open
539 * This function returns MTD device description structure in case of
540 * success and an error code in case of failure.
543 struct mtd_info
*get_mtd_device_nm(const char *name
)
546 struct mtd_info
*mtd
= NULL
, *other
;
548 mutex_lock(&mtd_table_mutex
);
550 mtd_for_each_device(other
) {
551 if (!strcmp(name
, other
->name
)) {
560 if (!try_module_get(mtd
->owner
))
563 if (mtd
->get_device
) {
564 err
= mtd
->get_device(mtd
);
570 mutex_unlock(&mtd_table_mutex
);
574 module_put(mtd
->owner
);
576 mutex_unlock(&mtd_table_mutex
);
580 void put_mtd_device(struct mtd_info
*mtd
)
582 mutex_lock(&mtd_table_mutex
);
583 __put_mtd_device(mtd
);
584 mutex_unlock(&mtd_table_mutex
);
588 void __put_mtd_device(struct mtd_info
*mtd
)
591 BUG_ON(mtd
->usecount
< 0);
594 mtd
->put_device(mtd
);
596 module_put(mtd
->owner
);
599 /* default_mtd_writev - default mtd writev method for MTD devices that
600 * don't implement their own
603 int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
604 unsigned long count
, loff_t to
, size_t *retlen
)
607 size_t totlen
= 0, thislen
;
613 for (i
=0; i
<count
; i
++) {
614 if (!vecs
[i
].iov_len
)
616 ret
= mtd
->write(mtd
, to
, vecs
[i
].iov_len
, &thislen
, vecs
[i
].iov_base
);
618 if (ret
|| thislen
!= vecs
[i
].iov_len
)
620 to
+= vecs
[i
].iov_len
;
628 EXPORT_SYMBOL_GPL(add_mtd_device
);
629 EXPORT_SYMBOL_GPL(del_mtd_device
);
630 EXPORT_SYMBOL_GPL(get_mtd_device
);
631 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
632 EXPORT_SYMBOL_GPL(__get_mtd_device
);
633 EXPORT_SYMBOL_GPL(put_mtd_device
);
634 EXPORT_SYMBOL_GPL(__put_mtd_device
);
635 EXPORT_SYMBOL_GPL(register_mtd_user
);
636 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
637 EXPORT_SYMBOL_GPL(default_mtd_writev
);
639 #ifdef CONFIG_PROC_FS
641 /*====================================================================*/
642 /* Support for /proc/mtd */
644 static struct proc_dir_entry
*proc_mtd
;
646 static inline int mtd_proc_info(char *buf
, struct mtd_info
*this)
648 return sprintf(buf
, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index
,
649 (unsigned long long)this->size
,
650 this->erasesize
, this->name
);
653 static int mtd_read_proc (char *page
, char **start
, off_t off
, int count
,
654 int *eof
, void *data_unused
)
656 struct mtd_info
*mtd
;
660 mutex_lock(&mtd_table_mutex
);
662 len
= sprintf(page
, "dev: size erasesize name\n");
663 mtd_for_each_device(mtd
) {
664 l
= mtd_proc_info(page
+ len
, mtd
);
666 if (len
+begin
> off
+count
)
668 if (len
+begin
< off
) {
677 mutex_unlock(&mtd_table_mutex
);
678 if (off
>= len
+begin
)
680 *start
= page
+ (off
-begin
);
681 return ((count
< begin
+len
-off
) ? count
: begin
+len
-off
);
684 #endif /* CONFIG_PROC_FS */
686 /*====================================================================*/
689 static int __init
mtd_bdi_init(struct backing_dev_info
*bdi
, const char *name
)
695 ret
= bdi_register(bdi
, NULL
, name
);
703 static int __init
init_mtd(void)
707 ret
= class_register(&mtd_class
);
711 ret
= mtd_bdi_init(&mtd_bdi_unmappable
, "mtd-unmap");
715 ret
= mtd_bdi_init(&mtd_bdi_ro_mappable
, "mtd-romap");
719 ret
= mtd_bdi_init(&mtd_bdi_rw_mappable
, "mtd-rwmap");
723 #ifdef CONFIG_PROC_FS
724 if ((proc_mtd
= create_proc_entry( "mtd", 0, NULL
)))
725 proc_mtd
->read_proc
= mtd_read_proc
;
726 #endif /* CONFIG_PROC_FS */
730 bdi_destroy(&mtd_bdi_ro_mappable
);
732 bdi_destroy(&mtd_bdi_unmappable
);
734 class_unregister(&mtd_class
);
736 pr_err("Error registering mtd class or bdi: %d\n", ret
);
740 static void __exit
cleanup_mtd(void)
742 #ifdef CONFIG_PROC_FS
744 remove_proc_entry( "mtd", NULL
);
745 #endif /* CONFIG_PROC_FS */
746 class_unregister(&mtd_class
);
747 bdi_destroy(&mtd_bdi_unmappable
);
748 bdi_destroy(&mtd_bdi_ro_mappable
);
749 bdi_destroy(&mtd_bdi_rw_mappable
);
752 module_init(init_mtd
);
753 module_exit(cleanup_mtd
);
755 MODULE_LICENSE("GPL");
756 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
757 MODULE_DESCRIPTION("Core MTD registration and access routines");