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 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/ptrace.h>
16 #include <linux/seq_file.h>
17 #include <linux/string.h>
18 #include <linux/timer.h>
19 #include <linux/major.h>
21 #include <linux/err.h>
22 #include <linux/ioctl.h>
23 #include <linux/init.h>
24 #include <linux/proc_fs.h>
25 #include <linux/idr.h>
26 #include <linux/backing-dev.h>
27 #include <linux/gfp.h>
28 #include <linux/slab.h>
30 #include <linux/err.h>
31 #include <ubi_uboot.h>
34 #include <linux/log2.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
42 * backing device capabilities for non-mappable devices (such as NAND flash)
43 * - permits private mappings, copies are taken of the data
45 static struct backing_dev_info mtd_bdi_unmappable
= {
46 .capabilities
= BDI_CAP_MAP_COPY
,
50 * backing device capabilities for R/O mappable devices (such as ROM)
51 * - permits private mappings, copies are taken of the data
52 * - permits non-writable shared mappings
54 static struct backing_dev_info mtd_bdi_ro_mappable
= {
55 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
56 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
),
60 * backing device capabilities for writable mappable devices (such as RAM)
61 * - permits private mappings, copies are taken of the data
62 * - permits non-writable shared mappings
64 static struct backing_dev_info mtd_bdi_rw_mappable
= {
65 .capabilities
= (BDI_CAP_MAP_COPY
| BDI_CAP_MAP_DIRECT
|
66 BDI_CAP_EXEC_MAP
| BDI_CAP_READ_MAP
|
70 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
);
71 static int mtd_cls_resume(struct device
*dev
);
73 static struct class mtd_class
= {
76 .suspend
= mtd_cls_suspend
,
77 .resume
= mtd_cls_resume
,
80 struct mtd_info
*mtd_table
[MAX_MTD_DEVICES
];
90 struct idr_layer id
[MAX_IDR_ID
];
93 #define DEFINE_IDR(name) struct idr name;
95 void idr_remove(struct idr
*idp
, int id
)
102 void *idr_find(struct idr
*idp
, int id
)
104 if (idp
->id
[id
].used
)
105 return idp
->id
[id
].ptr
;
110 void *idr_get_next(struct idr
*idp
, int *next
)
115 ret
= idr_find(idp
, id
);
118 if (!idp
->id
[id
].used
)
128 int idr_alloc(struct idr
*idp
, void *ptr
, int start
, int end
, gfp_t gfp_mask
)
130 struct idr_layer
*idl
;
133 while (i
< MAX_IDR_ID
) {
135 if (idl
->used
== 0) {
146 static DEFINE_IDR(mtd_idr
);
148 /* These are exported solely for the purpose of mtd_blkdevs.c. You
149 should not use them for _anything_ else */
150 DEFINE_MUTEX(mtd_table_mutex
);
151 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
153 struct mtd_info
*__mtd_next_device(int i
)
155 return idr_get_next(&mtd_idr
, &i
);
157 EXPORT_SYMBOL_GPL(__mtd_next_device
);
160 static LIST_HEAD(mtd_notifiers
);
163 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
165 /* REVISIT once MTD uses the driver model better, whoever allocates
166 * the mtd_info will probably want to use the release() hook...
168 static void mtd_release(struct device
*dev
)
170 struct mtd_info __maybe_unused
*mtd
= dev_get_drvdata(dev
);
171 dev_t index
= MTD_DEVT(mtd
->index
);
173 /* remove /dev/mtdXro node if needed */
175 device_destroy(&mtd_class
, index
+ 1);
178 static int mtd_cls_suspend(struct device
*dev
, pm_message_t state
)
180 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
182 return mtd
? mtd_suspend(mtd
) : 0;
185 static int mtd_cls_resume(struct device
*dev
)
187 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
194 static ssize_t
mtd_type_show(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
222 case MTD_MLCNANDFLASH
:
229 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
231 static DEVICE_ATTR(type
, S_IRUGO
, mtd_type_show
, NULL
);
233 static ssize_t
mtd_flags_show(struct device
*dev
,
234 struct device_attribute
*attr
, char *buf
)
236 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
238 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n", (unsigned long)mtd
->flags
);
241 static DEVICE_ATTR(flags
, S_IRUGO
, mtd_flags_show
, NULL
);
243 static ssize_t
mtd_size_show(struct device
*dev
,
244 struct device_attribute
*attr
, char *buf
)
246 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
248 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
249 (unsigned long long)mtd
->size
);
252 static DEVICE_ATTR(size
, S_IRUGO
, mtd_size_show
, NULL
);
254 static ssize_t
mtd_erasesize_show(struct device
*dev
,
255 struct device_attribute
*attr
, char *buf
)
257 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
259 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->erasesize
);
262 static DEVICE_ATTR(erasesize
, S_IRUGO
, mtd_erasesize_show
, NULL
);
264 static ssize_t
mtd_writesize_show(struct device
*dev
,
265 struct device_attribute
*attr
, char *buf
)
267 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
269 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->writesize
);
272 static DEVICE_ATTR(writesize
, S_IRUGO
, mtd_writesize_show
, NULL
);
274 static ssize_t
mtd_subpagesize_show(struct device
*dev
,
275 struct device_attribute
*attr
, char *buf
)
277 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
278 unsigned int subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
280 return snprintf(buf
, PAGE_SIZE
, "%u\n", subpagesize
);
283 static DEVICE_ATTR(subpagesize
, S_IRUGO
, mtd_subpagesize_show
, NULL
);
285 static ssize_t
mtd_oobsize_show(struct device
*dev
,
286 struct device_attribute
*attr
, char *buf
)
288 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
290 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->oobsize
);
293 static DEVICE_ATTR(oobsize
, S_IRUGO
, mtd_oobsize_show
, NULL
);
295 static ssize_t
mtd_numeraseregions_show(struct device
*dev
,
296 struct device_attribute
*attr
, char *buf
)
298 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
300 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->numeraseregions
);
303 static DEVICE_ATTR(numeraseregions
, S_IRUGO
, mtd_numeraseregions_show
,
306 static ssize_t
mtd_name_show(struct device
*dev
,
307 struct device_attribute
*attr
, char *buf
)
309 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
311 return snprintf(buf
, PAGE_SIZE
, "%s\n", mtd
->name
);
314 static DEVICE_ATTR(name
, S_IRUGO
, mtd_name_show
, NULL
);
316 static ssize_t
mtd_ecc_strength_show(struct device
*dev
,
317 struct device_attribute
*attr
, char *buf
)
319 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
321 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->ecc_strength
);
323 static DEVICE_ATTR(ecc_strength
, S_IRUGO
, mtd_ecc_strength_show
, NULL
);
325 static ssize_t
mtd_bitflip_threshold_show(struct device
*dev
,
326 struct device_attribute
*attr
,
329 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
331 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->bitflip_threshold
);
334 static ssize_t
mtd_bitflip_threshold_store(struct device
*dev
,
335 struct device_attribute
*attr
,
336 const char *buf
, size_t count
)
338 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
339 unsigned int bitflip_threshold
;
342 retval
= kstrtouint(buf
, 0, &bitflip_threshold
);
346 mtd
->bitflip_threshold
= bitflip_threshold
;
349 static DEVICE_ATTR(bitflip_threshold
, S_IRUGO
| S_IWUSR
,
350 mtd_bitflip_threshold_show
,
351 mtd_bitflip_threshold_store
);
353 static ssize_t
mtd_ecc_step_size_show(struct device
*dev
,
354 struct device_attribute
*attr
, char *buf
)
356 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
358 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->ecc_step_size
);
361 static DEVICE_ATTR(ecc_step_size
, S_IRUGO
, mtd_ecc_step_size_show
, NULL
);
363 static struct attribute
*mtd_attrs
[] = {
365 &dev_attr_flags
.attr
,
367 &dev_attr_erasesize
.attr
,
368 &dev_attr_writesize
.attr
,
369 &dev_attr_subpagesize
.attr
,
370 &dev_attr_oobsize
.attr
,
371 &dev_attr_numeraseregions
.attr
,
373 &dev_attr_ecc_strength
.attr
,
374 &dev_attr_ecc_step_size
.attr
,
375 &dev_attr_bitflip_threshold
.attr
,
378 ATTRIBUTE_GROUPS(mtd
);
380 static struct device_type mtd_devtype
= {
382 .groups
= mtd_groups
,
383 .release
= mtd_release
,
388 * add_mtd_device - register an MTD device
389 * @mtd: pointer to new MTD device info structure
391 * Add a device to the list of MTD devices present in the system, and
392 * notify each currently active MTD 'user' of its arrival. Returns
393 * zero on success or 1 on failure, which currently will only happen
394 * if there is insufficient memory or a sysfs error.
397 int add_mtd_device(struct mtd_info
*mtd
)
400 struct mtd_notifier
*not;
405 if (!mtd
->backing_dev_info
) {
408 mtd
->backing_dev_info
= &mtd_bdi_rw_mappable
;
411 mtd
->backing_dev_info
= &mtd_bdi_ro_mappable
;
414 mtd
->backing_dev_info
= &mtd_bdi_unmappable
;
420 BUG_ON(mtd
->writesize
== 0);
421 mutex_lock(&mtd_table_mutex
);
423 i
= idr_alloc(&mtd_idr
, mtd
, 0, 0, GFP_KERNEL
);
430 /* default value if not set by driver */
431 if (mtd
->bitflip_threshold
== 0)
432 mtd
->bitflip_threshold
= mtd
->ecc_strength
;
434 if (is_power_of_2(mtd
->erasesize
))
435 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
437 mtd
->erasesize_shift
= 0;
439 if (is_power_of_2(mtd
->writesize
))
440 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
442 mtd
->writesize_shift
= 0;
444 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
445 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
447 /* Some chips always power up locked. Unlock them now */
448 if ((mtd
->flags
& MTD_WRITEABLE
) && (mtd
->flags
& MTD_POWERUP_LOCK
)) {
449 error
= mtd_unlock(mtd
, 0, mtd
->size
);
450 if (error
&& error
!= -EOPNOTSUPP
)
452 "%s: unlock failed, writes may not work\n",
457 /* Caller should have set dev.parent to match the
460 mtd
->dev
.type
= &mtd_devtype
;
461 mtd
->dev
.class = &mtd_class
;
462 mtd
->dev
.devt
= MTD_DEVT(i
);
463 dev_set_name(&mtd
->dev
, "mtd%d", i
);
464 dev_set_drvdata(&mtd
->dev
, mtd
);
465 if (device_register(&mtd
->dev
) != 0)
469 device_create(&mtd_class
, mtd
->dev
.parent
,
473 pr_debug("mtd: Giving out device %d to %s\n", i
, mtd
->name
);
474 /* No need to get a refcount on the module containing
475 the notifier, since we hold the mtd_table_mutex */
476 list_for_each_entry(not, &mtd_notifiers
, list
)
479 pr_debug("mtd: Giving out device %d to %s\n", i
, mtd
->name
);
482 mutex_unlock(&mtd_table_mutex
);
483 /* We _know_ we aren't being removed, because
484 our caller is still holding us here. So none
485 of this try_ nonsense, and no bitching about it
487 __module_get(THIS_MODULE
);
492 idr_remove(&mtd_idr
, i
);
495 mutex_unlock(&mtd_table_mutex
);
500 * del_mtd_device - unregister an MTD device
501 * @mtd: pointer to MTD device info structure
503 * Remove a device from the list of MTD devices present in the system,
504 * and notify each currently active MTD 'user' of its departure.
505 * Returns zero on success or 1 on failure, which currently will happen
506 * if the requested device does not appear to be present in the list.
509 int del_mtd_device(struct mtd_info
*mtd
)
513 struct mtd_notifier
*not;
516 mutex_lock(&mtd_table_mutex
);
518 if (idr_find(&mtd_idr
, mtd
->index
) != mtd
) {
524 /* No need to get a refcount on the module containing
525 the notifier, since we hold the mtd_table_mutex */
526 list_for_each_entry(not, &mtd_notifiers
, list
)
531 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
532 mtd
->index
, mtd
->name
, mtd
->usecount
);
536 device_unregister(&mtd
->dev
);
539 idr_remove(&mtd_idr
, mtd
->index
);
541 module_put(THIS_MODULE
);
546 mutex_unlock(&mtd_table_mutex
);
552 * mtd_device_parse_register - parse partitions and register an MTD device.
554 * @mtd: the MTD device to register
555 * @types: the list of MTD partition probes to try, see
556 * 'parse_mtd_partitions()' for more information
557 * @parser_data: MTD partition parser-specific data
558 * @parts: fallback partition information to register, if parsing fails;
559 * only valid if %nr_parts > %0
560 * @nr_parts: the number of partitions in parts, if zero then the full
561 * MTD device is registered if no partition info is found
563 * This function aggregates MTD partitions parsing (done by
564 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
565 * basically follows the most common pattern found in many MTD drivers:
567 * * It first tries to probe partitions on MTD device @mtd using parsers
568 * specified in @types (if @types is %NULL, then the default list of parsers
569 * is used, see 'parse_mtd_partitions()' for more information). If none are
570 * found this functions tries to fallback to information specified in
572 * * If any partitioning info was found, this function registers the found
574 * * If no partitions were found this function just registers the MTD device
577 * Returns zero in case of success and a negative error code in case of failure.
579 int mtd_device_parse_register(struct mtd_info
*mtd
, const char * const *types
,
580 struct mtd_part_parser_data
*parser_data
,
581 const struct mtd_partition
*parts
,
585 struct mtd_partition
*real_parts
;
587 err
= parse_mtd_partitions(mtd
, types
, &real_parts
, parser_data
);
588 if (err
<= 0 && nr_parts
&& parts
) {
589 real_parts
= kmemdup(parts
, sizeof(*parts
) * nr_parts
,
598 err
= add_mtd_partitions(mtd
, real_parts
, err
);
600 } else if (err
== 0) {
601 err
= add_mtd_device(mtd
);
608 EXPORT_SYMBOL_GPL(mtd_device_parse_register
);
611 * mtd_device_unregister - unregister an existing MTD device.
613 * @master: the MTD device to unregister. This will unregister both the master
614 * and any partitions if registered.
616 int mtd_device_unregister(struct mtd_info
*master
)
620 err
= del_mtd_partitions(master
);
624 if (!device_is_registered(&master
->dev
))
627 return del_mtd_device(master
);
629 EXPORT_SYMBOL_GPL(mtd_device_unregister
);
632 * register_mtd_user - register a 'user' of MTD devices.
633 * @new: pointer to notifier info structure
635 * Registers a pair of callbacks function to be called upon addition
636 * or removal of MTD devices. Causes the 'add' callback to be immediately
637 * invoked for each MTD device currently present in the system.
639 void register_mtd_user (struct mtd_notifier
*new)
641 struct mtd_info
*mtd
;
643 mutex_lock(&mtd_table_mutex
);
645 list_add(&new->list
, &mtd_notifiers
);
647 __module_get(THIS_MODULE
);
649 mtd_for_each_device(mtd
)
652 mutex_unlock(&mtd_table_mutex
);
654 EXPORT_SYMBOL_GPL(register_mtd_user
);
657 * unregister_mtd_user - unregister a 'user' of MTD devices.
658 * @old: pointer to notifier info structure
660 * Removes a callback function pair from the list of 'users' to be
661 * notified upon addition or removal of MTD devices. Causes the
662 * 'remove' callback to be immediately invoked for each MTD device
663 * currently present in the system.
665 int unregister_mtd_user (struct mtd_notifier
*old
)
667 struct mtd_info
*mtd
;
669 mutex_lock(&mtd_table_mutex
);
671 module_put(THIS_MODULE
);
673 mtd_for_each_device(mtd
)
676 list_del(&old
->list
);
677 mutex_unlock(&mtd_table_mutex
);
680 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
684 * get_mtd_device - obtain a validated handle for an MTD device
685 * @mtd: last known address of the required MTD device
686 * @num: internal device number of the required MTD device
688 * Given a number and NULL address, return the num'th entry in the device
689 * table, if any. Given an address and num == -1, search the device table
690 * for a device with that address and return if it's still present. Given
691 * both, return the num'th driver only if its address matches. Return
694 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
696 struct mtd_info
*ret
= NULL
, *other
;
699 mutex_lock(&mtd_table_mutex
);
702 mtd_for_each_device(other
) {
708 } else if (num
>= 0) {
709 ret
= idr_find(&mtd_idr
, num
);
710 if (mtd
&& mtd
!= ret
)
719 err
= __get_mtd_device(ret
);
723 mutex_unlock(&mtd_table_mutex
);
726 EXPORT_SYMBOL_GPL(get_mtd_device
);
729 int __get_mtd_device(struct mtd_info
*mtd
)
733 if (!try_module_get(mtd
->owner
))
736 if (mtd
->_get_device
) {
737 err
= mtd
->_get_device(mtd
);
740 module_put(mtd
->owner
);
747 EXPORT_SYMBOL_GPL(__get_mtd_device
);
750 * get_mtd_device_nm - obtain a validated handle for an MTD device by
752 * @name: MTD device name to open
754 * This function returns MTD device description structure in case of
755 * success and an error code in case of failure.
757 struct mtd_info
*get_mtd_device_nm(const char *name
)
760 struct mtd_info
*mtd
= NULL
, *other
;
762 mutex_lock(&mtd_table_mutex
);
764 mtd_for_each_device(other
) {
765 if (!strcmp(name
, other
->name
)) {
774 err
= __get_mtd_device(mtd
);
778 mutex_unlock(&mtd_table_mutex
);
782 mutex_unlock(&mtd_table_mutex
);
785 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
787 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
789 * mtd_get_len_incl_bad
791 * Check if length including bad blocks fits into device.
793 * @param mtd an MTD device
794 * @param offset offset in flash
795 * @param length image length
796 * @return image length including bad blocks in *len_incl_bad and whether or not
797 * the length returned was truncated in *truncated
799 void mtd_get_len_incl_bad(struct mtd_info
*mtd
, uint64_t offset
,
800 const uint64_t length
, uint64_t *len_incl_bad
,
806 if (!mtd
->_block_isbad
) {
807 *len_incl_bad
= length
;
811 uint64_t len_excl_bad
= 0;
814 while (len_excl_bad
< length
) {
815 if (offset
>= mtd
->size
) {
820 block_len
= mtd
->erasesize
- (offset
& (mtd
->erasesize
- 1));
822 if (!mtd
->_block_isbad(mtd
, offset
& ~(mtd
->erasesize
- 1)))
823 len_excl_bad
+= block_len
;
825 *len_incl_bad
+= block_len
;
829 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
831 void put_mtd_device(struct mtd_info
*mtd
)
833 mutex_lock(&mtd_table_mutex
);
834 __put_mtd_device(mtd
);
835 mutex_unlock(&mtd_table_mutex
);
838 EXPORT_SYMBOL_GPL(put_mtd_device
);
840 void __put_mtd_device(struct mtd_info
*mtd
)
843 BUG_ON(mtd
->usecount
< 0);
845 if (mtd
->_put_device
)
846 mtd
->_put_device(mtd
);
848 module_put(mtd
->owner
);
850 EXPORT_SYMBOL_GPL(__put_mtd_device
);
853 * Erase is an asynchronous operation. Device drivers are supposed
854 * to call instr->callback() whenever the operation completes, even
855 * if it completes with a failure.
856 * Callers are supposed to pass a callback function and wait for it
857 * to be called before writing to the block.
859 int mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
861 if (instr
->addr
> mtd
->size
|| instr
->len
> mtd
->size
- instr
->addr
)
863 if (!(mtd
->flags
& MTD_WRITEABLE
))
865 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
867 instr
->state
= MTD_ERASE_DONE
;
868 mtd_erase_callback(instr
);
871 return mtd
->_erase(mtd
, instr
);
873 EXPORT_SYMBOL_GPL(mtd_erase
);
877 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
879 int mtd_point(struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
,
880 void **virt
, resource_size_t
*phys
)
888 if (from
< 0 || from
> mtd
->size
|| len
> mtd
->size
- from
)
892 return mtd
->_point(mtd
, from
, len
, retlen
, virt
, phys
);
894 EXPORT_SYMBOL_GPL(mtd_point
);
896 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
897 int mtd_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
901 if (from
< 0 || from
> mtd
->size
|| len
> mtd
->size
- from
)
905 return mtd
->_unpoint(mtd
, from
, len
);
907 EXPORT_SYMBOL_GPL(mtd_unpoint
);
911 * Allow NOMMU mmap() to directly map the device (if not NULL)
912 * - return the address to which the offset maps
913 * - return -ENOSYS to indicate refusal to do the mapping
915 unsigned long mtd_get_unmapped_area(struct mtd_info
*mtd
, unsigned long len
,
916 unsigned long offset
, unsigned long flags
)
918 if (!mtd
->_get_unmapped_area
)
920 if (offset
> mtd
->size
|| len
> mtd
->size
- offset
)
922 return mtd
->_get_unmapped_area(mtd
, len
, offset
, flags
);
924 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area
);
926 int mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
,
931 if (from
< 0 || from
> mtd
->size
|| len
> mtd
->size
- from
)
937 * In the absence of an error, drivers return a non-negative integer
938 * representing the maximum number of bitflips that were corrected on
939 * any one ecc region (if applicable; zero otherwise).
941 ret_code
= mtd
->_read(mtd
, from
, len
, retlen
, buf
);
942 if (unlikely(ret_code
< 0))
944 if (mtd
->ecc_strength
== 0)
945 return 0; /* device lacks ecc */
946 return ret_code
>= mtd
->bitflip_threshold
? -EUCLEAN
: 0;
948 EXPORT_SYMBOL_GPL(mtd_read
);
950 int mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
,
954 if (to
< 0 || to
> mtd
->size
|| len
> mtd
->size
- to
)
956 if (!mtd
->_write
|| !(mtd
->flags
& MTD_WRITEABLE
))
960 return mtd
->_write(mtd
, to
, len
, retlen
, buf
);
962 EXPORT_SYMBOL_GPL(mtd_write
);
965 * In blackbox flight recorder like scenarios we want to make successful writes
966 * in interrupt context. panic_write() is only intended to be called when its
967 * known the kernel is about to panic and we need the write to succeed. Since
968 * the kernel is not going to be running for much longer, this function can
969 * break locks and delay to ensure the write succeeds (but not sleep).
971 int mtd_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
,
975 if (!mtd
->_panic_write
)
977 if (to
< 0 || to
> mtd
->size
|| len
> mtd
->size
- to
)
979 if (!(mtd
->flags
& MTD_WRITEABLE
))
983 return mtd
->_panic_write(mtd
, to
, len
, retlen
, buf
);
985 EXPORT_SYMBOL_GPL(mtd_panic_write
);
987 int mtd_read_oob(struct mtd_info
*mtd
, loff_t from
, struct mtd_oob_ops
*ops
)
990 ops
->retlen
= ops
->oobretlen
= 0;
994 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
995 * similar to mtd->_read(), returning a non-negative integer
996 * representing max bitflips. In other cases, mtd->_read_oob() may
997 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
999 ret_code
= mtd
->_read_oob(mtd
, from
, ops
);
1000 if (unlikely(ret_code
< 0))
1002 if (mtd
->ecc_strength
== 0)
1003 return 0; /* device lacks ecc */
1004 return ret_code
>= mtd
->bitflip_threshold
? -EUCLEAN
: 0;
1006 EXPORT_SYMBOL_GPL(mtd_read_oob
);
1009 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1010 * @mtd: MTD device structure
1011 * @section: ECC section. Depending on the layout you may have all the ECC
1012 * bytes stored in a single contiguous section, or one section
1013 * per ECC chunk (and sometime several sections for a single ECC
1015 * @oobecc: OOB region struct filled with the appropriate ECC position
1018 * This function returns ECC section information in the OOB area. If you want
1019 * to get all the ECC bytes information, then you should call
1020 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1022 * Returns zero on success, a negative error code otherwise.
1024 int mtd_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
1025 struct mtd_oob_region
*oobecc
)
1027 memset(oobecc
, 0, sizeof(*oobecc
));
1029 if (!mtd
|| section
< 0)
1032 if (!mtd
->ooblayout
|| !mtd
->ooblayout
->ecc
)
1035 return mtd
->ooblayout
->ecc(mtd
, section
, oobecc
);
1037 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc
);
1040 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1042 * @mtd: MTD device structure
1043 * @section: Free section you are interested in. Depending on the layout
1044 * you may have all the free bytes stored in a single contiguous
1045 * section, or one section per ECC chunk plus an extra section
1046 * for the remaining bytes (or other funky layout).
1047 * @oobfree: OOB region struct filled with the appropriate free position
1050 * This function returns free bytes position in the OOB area. If you want
1051 * to get all the free bytes information, then you should call
1052 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1054 * Returns zero on success, a negative error code otherwise.
1056 int mtd_ooblayout_free(struct mtd_info
*mtd
, int section
,
1057 struct mtd_oob_region
*oobfree
)
1059 memset(oobfree
, 0, sizeof(*oobfree
));
1061 if (!mtd
|| section
< 0)
1064 if (!mtd
->ooblayout
|| !mtd
->ooblayout
->free
)
1067 return mtd
->ooblayout
->free(mtd
, section
, oobfree
);
1069 EXPORT_SYMBOL_GPL(mtd_ooblayout_free
);
1072 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1073 * @mtd: mtd info structure
1074 * @byte: the byte we are searching for
1075 * @sectionp: pointer where the section id will be stored
1076 * @oobregion: used to retrieve the ECC position
1077 * @iter: iterator function. Should be either mtd_ooblayout_free or
1078 * mtd_ooblayout_ecc depending on the region type you're searching for
1080 * This function returns the section id and oobregion information of a
1081 * specific byte. For example, say you want to know where the 4th ECC byte is
1082 * stored, you'll use:
1084 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1086 * Returns zero on success, a negative error code otherwise.
1088 static int mtd_ooblayout_find_region(struct mtd_info
*mtd
, int byte
,
1089 int *sectionp
, struct mtd_oob_region
*oobregion
,
1090 int (*iter
)(struct mtd_info
*,
1092 struct mtd_oob_region
*oobregion
))
1094 int pos
= 0, ret
, section
= 0;
1096 memset(oobregion
, 0, sizeof(*oobregion
));
1099 ret
= iter(mtd
, section
, oobregion
);
1103 if (pos
+ oobregion
->length
> byte
)
1106 pos
+= oobregion
->length
;
1111 * Adjust region info to make it start at the beginning at the
1114 oobregion
->offset
+= byte
- pos
;
1115 oobregion
->length
-= byte
- pos
;
1116 *sectionp
= section
;
1122 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1124 * @mtd: mtd info structure
1125 * @eccbyte: the byte we are searching for
1126 * @sectionp: pointer where the section id will be stored
1127 * @oobregion: OOB region information
1129 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1132 * Returns zero on success, a negative error code otherwise.
1134 int mtd_ooblayout_find_eccregion(struct mtd_info
*mtd
, int eccbyte
,
1136 struct mtd_oob_region
*oobregion
)
1138 return mtd_ooblayout_find_region(mtd
, eccbyte
, section
, oobregion
,
1141 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion
);
1144 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1145 * @mtd: mtd info structure
1146 * @buf: destination buffer to store OOB bytes
1147 * @oobbuf: OOB buffer
1148 * @start: first byte to retrieve
1149 * @nbytes: number of bytes to retrieve
1150 * @iter: section iterator
1152 * Extract bytes attached to a specific category (ECC or free)
1153 * from the OOB buffer and copy them into buf.
1155 * Returns zero on success, a negative error code otherwise.
1157 static int mtd_ooblayout_get_bytes(struct mtd_info
*mtd
, u8
*buf
,
1158 const u8
*oobbuf
, int start
, int nbytes
,
1159 int (*iter
)(struct mtd_info
*,
1161 struct mtd_oob_region
*oobregion
))
1163 struct mtd_oob_region oobregion
;
1166 ret
= mtd_ooblayout_find_region(mtd
, start
, §ion
,
1172 cnt
= min_t(int, nbytes
, oobregion
.length
);
1173 memcpy(buf
, oobbuf
+ oobregion
.offset
, cnt
);
1180 ret
= iter(mtd
, ++section
, &oobregion
);
1187 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1188 * @mtd: mtd info structure
1189 * @buf: source buffer to get OOB bytes from
1190 * @oobbuf: OOB buffer
1191 * @start: first OOB byte to set
1192 * @nbytes: number of OOB bytes to set
1193 * @iter: section iterator
1195 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1196 * is selected by passing the appropriate iterator.
1198 * Returns zero on success, a negative error code otherwise.
1200 static int mtd_ooblayout_set_bytes(struct mtd_info
*mtd
, const u8
*buf
,
1201 u8
*oobbuf
, int start
, int nbytes
,
1202 int (*iter
)(struct mtd_info
*,
1204 struct mtd_oob_region
*oobregion
))
1206 struct mtd_oob_region oobregion
;
1209 ret
= mtd_ooblayout_find_region(mtd
, start
, §ion
,
1215 cnt
= min_t(int, nbytes
, oobregion
.length
);
1216 memcpy(oobbuf
+ oobregion
.offset
, buf
, cnt
);
1223 ret
= iter(mtd
, ++section
, &oobregion
);
1230 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1231 * @mtd: mtd info structure
1232 * @iter: category iterator
1234 * Count the number of bytes in a given category.
1236 * Returns a positive value on success, a negative error code otherwise.
1238 static int mtd_ooblayout_count_bytes(struct mtd_info
*mtd
,
1239 int (*iter
)(struct mtd_info
*,
1241 struct mtd_oob_region
*oobregion
))
1243 struct mtd_oob_region oobregion
;
1244 int section
= 0, ret
, nbytes
= 0;
1247 ret
= iter(mtd
, section
++, &oobregion
);
1254 nbytes
+= oobregion
.length
;
1261 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1262 * @mtd: mtd info structure
1263 * @eccbuf: destination buffer to store ECC bytes
1264 * @oobbuf: OOB buffer
1265 * @start: first ECC byte to retrieve
1266 * @nbytes: number of ECC bytes to retrieve
1268 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1270 * Returns zero on success, a negative error code otherwise.
1272 int mtd_ooblayout_get_eccbytes(struct mtd_info
*mtd
, u8
*eccbuf
,
1273 const u8
*oobbuf
, int start
, int nbytes
)
1275 return mtd_ooblayout_get_bytes(mtd
, eccbuf
, oobbuf
, start
, nbytes
,
1278 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes
);
1281 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1282 * @mtd: mtd info structure
1283 * @eccbuf: source buffer to get ECC bytes from
1284 * @oobbuf: OOB buffer
1285 * @start: first ECC byte to set
1286 * @nbytes: number of ECC bytes to set
1288 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1290 * Returns zero on success, a negative error code otherwise.
1292 int mtd_ooblayout_set_eccbytes(struct mtd_info
*mtd
, const u8
*eccbuf
,
1293 u8
*oobbuf
, int start
, int nbytes
)
1295 return mtd_ooblayout_set_bytes(mtd
, eccbuf
, oobbuf
, start
, nbytes
,
1298 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes
);
1301 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1302 * @mtd: mtd info structure
1303 * @databuf: destination buffer to store ECC bytes
1304 * @oobbuf: OOB buffer
1305 * @start: first ECC byte to retrieve
1306 * @nbytes: number of ECC bytes to retrieve
1308 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1310 * Returns zero on success, a negative error code otherwise.
1312 int mtd_ooblayout_get_databytes(struct mtd_info
*mtd
, u8
*databuf
,
1313 const u8
*oobbuf
, int start
, int nbytes
)
1315 return mtd_ooblayout_get_bytes(mtd
, databuf
, oobbuf
, start
, nbytes
,
1316 mtd_ooblayout_free
);
1318 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes
);
1321 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1322 * @mtd: mtd info structure
1323 * @eccbuf: source buffer to get data bytes from
1324 * @oobbuf: OOB buffer
1325 * @start: first ECC byte to set
1326 * @nbytes: number of ECC bytes to set
1328 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1330 * Returns zero on success, a negative error code otherwise.
1332 int mtd_ooblayout_set_databytes(struct mtd_info
*mtd
, const u8
*databuf
,
1333 u8
*oobbuf
, int start
, int nbytes
)
1335 return mtd_ooblayout_set_bytes(mtd
, databuf
, oobbuf
, start
, nbytes
,
1336 mtd_ooblayout_free
);
1338 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes
);
1341 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1342 * @mtd: mtd info structure
1344 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1346 * Returns zero on success, a negative error code otherwise.
1348 int mtd_ooblayout_count_freebytes(struct mtd_info
*mtd
)
1350 return mtd_ooblayout_count_bytes(mtd
, mtd_ooblayout_free
);
1352 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes
);
1355 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1356 * @mtd: mtd info structure
1358 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1360 * Returns zero on success, a negative error code otherwise.
1362 int mtd_ooblayout_count_eccbytes(struct mtd_info
*mtd
)
1364 return mtd_ooblayout_count_bytes(mtd
, mtd_ooblayout_ecc
);
1366 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes
);
1369 * Method to access the protection register area, present in some flash
1370 * devices. The user data is one time programmable but the factory data is read
1373 int mtd_get_fact_prot_info(struct mtd_info
*mtd
, size_t len
, size_t *retlen
,
1374 struct otp_info
*buf
)
1376 if (!mtd
->_get_fact_prot_info
)
1380 return mtd
->_get_fact_prot_info(mtd
, len
, retlen
, buf
);
1382 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info
);
1384 int mtd_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1385 size_t *retlen
, u_char
*buf
)
1388 if (!mtd
->_read_fact_prot_reg
)
1392 return mtd
->_read_fact_prot_reg(mtd
, from
, len
, retlen
, buf
);
1394 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg
);
1396 int mtd_get_user_prot_info(struct mtd_info
*mtd
, size_t len
, size_t *retlen
,
1397 struct otp_info
*buf
)
1399 if (!mtd
->_get_user_prot_info
)
1403 return mtd
->_get_user_prot_info(mtd
, len
, retlen
, buf
);
1405 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info
);
1407 int mtd_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1408 size_t *retlen
, u_char
*buf
)
1411 if (!mtd
->_read_user_prot_reg
)
1415 return mtd
->_read_user_prot_reg(mtd
, from
, len
, retlen
, buf
);
1417 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg
);
1419 int mtd_write_user_prot_reg(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1420 size_t *retlen
, u_char
*buf
)
1425 if (!mtd
->_write_user_prot_reg
)
1429 ret
= mtd
->_write_user_prot_reg(mtd
, to
, len
, retlen
, buf
);
1434 * If no data could be written at all, we are out of memory and
1435 * must return -ENOSPC.
1437 return (*retlen
) ? 0 : -ENOSPC
;
1439 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg
);
1441 int mtd_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
)
1443 if (!mtd
->_lock_user_prot_reg
)
1447 return mtd
->_lock_user_prot_reg(mtd
, from
, len
);
1449 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg
);
1451 /* Chip-supported device locking */
1452 int mtd_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1456 if (ofs
< 0 || ofs
> mtd
->size
|| len
> mtd
->size
- ofs
)
1460 return mtd
->_lock(mtd
, ofs
, len
);
1462 EXPORT_SYMBOL_GPL(mtd_lock
);
1464 int mtd_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1468 if (ofs
< 0 || ofs
> mtd
->size
|| len
> mtd
->size
- ofs
)
1472 return mtd
->_unlock(mtd
, ofs
, len
);
1474 EXPORT_SYMBOL_GPL(mtd_unlock
);
1476 int mtd_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1478 if (!mtd
->_is_locked
)
1480 if (ofs
< 0 || ofs
> mtd
->size
|| len
> mtd
->size
- ofs
)
1484 return mtd
->_is_locked(mtd
, ofs
, len
);
1486 EXPORT_SYMBOL_GPL(mtd_is_locked
);
1488 int mtd_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
1490 if (ofs
< 0 || ofs
> mtd
->size
)
1492 if (!mtd
->_block_isreserved
)
1494 return mtd
->_block_isreserved(mtd
, ofs
);
1496 EXPORT_SYMBOL_GPL(mtd_block_isreserved
);
1498 int mtd_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
1500 if (ofs
< 0 || ofs
> mtd
->size
)
1502 if (!mtd
->_block_isbad
)
1504 return mtd
->_block_isbad(mtd
, ofs
);
1506 EXPORT_SYMBOL_GPL(mtd_block_isbad
);
1508 int mtd_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1510 if (!mtd
->_block_markbad
)
1512 if (ofs
< 0 || ofs
> mtd
->size
)
1514 if (!(mtd
->flags
& MTD_WRITEABLE
))
1516 return mtd
->_block_markbad(mtd
, ofs
);
1518 EXPORT_SYMBOL_GPL(mtd_block_markbad
);
1522 * default_mtd_writev - the default writev method
1523 * @mtd: mtd device description object pointer
1524 * @vecs: the vectors to write
1525 * @count: count of vectors in @vecs
1526 * @to: the MTD device offset to write to
1527 * @retlen: on exit contains the count of bytes written to the MTD device.
1529 * This function returns zero in case of success and a negative error code in
1532 static int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1533 unsigned long count
, loff_t to
, size_t *retlen
)
1536 size_t totlen
= 0, thislen
;
1539 for (i
= 0; i
< count
; i
++) {
1540 if (!vecs
[i
].iov_len
)
1542 ret
= mtd_write(mtd
, to
, vecs
[i
].iov_len
, &thislen
,
1545 if (ret
|| thislen
!= vecs
[i
].iov_len
)
1547 to
+= vecs
[i
].iov_len
;
1554 * mtd_writev - the vector-based MTD write method
1555 * @mtd: mtd device description object pointer
1556 * @vecs: the vectors to write
1557 * @count: count of vectors in @vecs
1558 * @to: the MTD device offset to write to
1559 * @retlen: on exit contains the count of bytes written to the MTD device.
1561 * This function returns zero in case of success and a negative error code in
1564 int mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1565 unsigned long count
, loff_t to
, size_t *retlen
)
1568 if (!(mtd
->flags
& MTD_WRITEABLE
))
1571 return default_mtd_writev(mtd
, vecs
, count
, to
, retlen
);
1572 return mtd
->_writev(mtd
, vecs
, count
, to
, retlen
);
1574 EXPORT_SYMBOL_GPL(mtd_writev
);
1577 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1578 * @mtd: mtd device description object pointer
1579 * @size: a pointer to the ideal or maximum size of the allocation, points
1580 * to the actual allocation size on success.
1582 * This routine attempts to allocate a contiguous kernel buffer up to
1583 * the specified size, backing off the size of the request exponentially
1584 * until the request succeeds or until the allocation size falls below
1585 * the system page size. This attempts to make sure it does not adversely
1586 * impact system performance, so when allocating more than one page, we
1587 * ask the memory allocator to avoid re-trying, swapping, writing back
1588 * or performing I/O.
1590 * Note, this function also makes sure that the allocated buffer is aligned to
1591 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1593 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1594 * to handle smaller (i.e. degraded) buffer allocations under low- or
1595 * fragmented-memory situations where such reduced allocations, from a
1596 * requested ideal, are allowed.
1598 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1600 void *mtd_kmalloc_up_to(const struct mtd_info
*mtd
, size_t *size
)
1602 gfp_t flags
= __GFP_NOWARN
| __GFP_WAIT
|
1603 __GFP_NORETRY
| __GFP_NO_KSWAPD
;
1604 size_t min_alloc
= max_t(size_t, mtd
->writesize
, PAGE_SIZE
);
1607 *size
= min_t(size_t, *size
, KMALLOC_MAX_SIZE
);
1609 while (*size
> min_alloc
) {
1610 kbuf
= kmalloc(*size
, flags
);
1615 *size
= ALIGN(*size
, mtd
->writesize
);
1619 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1620 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1622 return kmalloc(*size
, GFP_KERNEL
);
1624 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to
);
1627 #ifdef CONFIG_PROC_FS
1629 /*====================================================================*/
1630 /* Support for /proc/mtd */
1632 static int mtd_proc_show(struct seq_file
*m
, void *v
)
1634 struct mtd_info
*mtd
;
1636 seq_puts(m
, "dev: size erasesize name\n");
1637 mutex_lock(&mtd_table_mutex
);
1638 mtd_for_each_device(mtd
) {
1639 seq_printf(m
, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1640 mtd
->index
, (unsigned long long)mtd
->size
,
1641 mtd
->erasesize
, mtd
->name
);
1643 mutex_unlock(&mtd_table_mutex
);
1647 static int mtd_proc_open(struct inode
*inode
, struct file
*file
)
1649 return single_open(file
, mtd_proc_show
, NULL
);
1652 static const struct file_operations mtd_proc_ops
= {
1653 .open
= mtd_proc_open
,
1655 .llseek
= seq_lseek
,
1656 .release
= single_release
,
1658 #endif /* CONFIG_PROC_FS */
1660 /*====================================================================*/
1664 static int __init
mtd_bdi_init(struct backing_dev_info
*bdi
, const char *name
)
1668 ret
= bdi_init(bdi
);
1670 ret
= bdi_register(bdi
, NULL
, "%s", name
);
1678 static struct proc_dir_entry
*proc_mtd
;
1680 static int __init
init_mtd(void)
1684 ret
= class_register(&mtd_class
);
1688 ret
= mtd_bdi_init(&mtd_bdi_unmappable
, "mtd-unmap");
1692 ret
= mtd_bdi_init(&mtd_bdi_ro_mappable
, "mtd-romap");
1696 ret
= mtd_bdi_init(&mtd_bdi_rw_mappable
, "mtd-rwmap");
1700 proc_mtd
= proc_create("mtd", 0, NULL
, &mtd_proc_ops
);
1702 ret
= init_mtdchar();
1710 remove_proc_entry("mtd", NULL
);
1712 bdi_destroy(&mtd_bdi_ro_mappable
);
1714 bdi_destroy(&mtd_bdi_unmappable
);
1716 class_unregister(&mtd_class
);
1718 pr_err("Error registering mtd class or bdi: %d\n", ret
);
1722 static void __exit
cleanup_mtd(void)
1726 remove_proc_entry("mtd", NULL
);
1727 class_unregister(&mtd_class
);
1728 bdi_destroy(&mtd_bdi_unmappable
);
1729 bdi_destroy(&mtd_bdi_ro_mappable
);
1730 bdi_destroy(&mtd_bdi_rw_mappable
);
1733 module_init(init_mtd
);
1734 module_exit(cleanup_mtd
);
1737 MODULE_LICENSE("GPL");
1738 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1739 MODULE_DESCRIPTION("Core MTD registration and access routines");