1 // SPDX-License-Identifier: GPL-2.0+
3 * Core registration and callback routines for MTD
7 * Copyright © 2006 Red Hat UK Limited
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/ptrace.h>
15 #include <linux/seq_file.h>
16 #include <linux/string.h>
17 #include <linux/timer.h>
18 #include <linux/major.h>
20 #include <linux/err.h>
21 #include <linux/ioctl.h>
22 #include <linux/init.h>
23 #include <linux/proc_fs.h>
24 #include <linux/idr.h>
25 #include <linux/backing-dev.h>
26 #include <linux/gfp.h>
27 #include <linux/slab.h>
29 #include <linux/bitops.h>
30 #include <linux/bug.h>
31 #include <linux/err.h>
32 #include <ubi_uboot.h>
35 #include <linux/log2.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.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,
89 struct idr_layer id[MAX_IDR_ID];
93 #define DEFINE_IDR(name) struct idr name;
95 void idr_remove(struct idr *idp, int id)
97 if (idp->id[id].used) {
104 void *idr_find(struct idr *idp, int id)
106 if (idp->id[id].used)
107 return idp->id[id].ptr;
112 void *idr_get_next(struct idr *idp, int *next)
117 ret = idr_find(idp, id);
120 if (!idp->id[id].used)
130 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
132 struct idr_layer *idl;
135 while (i < MAX_IDR_ID) {
137 if (idl->used == 0) {
149 static DEFINE_IDR(mtd_idr);
151 /* These are exported solely for the purpose of mtd_blkdevs.c. You
152 should not use them for _anything_ else */
153 DEFINE_MUTEX(mtd_table_mutex);
154 EXPORT_SYMBOL_GPL(mtd_table_mutex);
156 struct mtd_info *__mtd_next_device(int i)
158 return idr_get_next(&mtd_idr, &i);
160 EXPORT_SYMBOL_GPL(__mtd_next_device);
162 bool mtd_dev_list_updated(void)
164 if (mtd_idr.updated) {
165 mtd_idr.updated = false;
173 static LIST_HEAD(mtd_notifiers);
175 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
177 /* REVISIT once MTD uses the driver model better, whoever allocates
178 * the mtd_info will probably want to use the release() hook...
180 static void mtd_release(struct device *dev)
182 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
183 dev_t index = MTD_DEVT(mtd->index);
185 /* remove /dev/mtdXro node if needed */
187 device_destroy(&mtd_class, index + 1);
190 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
192 struct mtd_info *mtd = dev_get_drvdata(dev);
194 return mtd ? mtd_suspend(mtd) : 0;
197 static int mtd_cls_resume(struct device *dev)
199 struct mtd_info *mtd = dev_get_drvdata(dev);
206 static ssize_t mtd_type_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 struct mtd_info *mtd = dev_get_drvdata(dev);
234 case MTD_MLCNANDFLASH:
241 return snprintf(buf, PAGE_SIZE, "%s\n", type);
243 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
245 static ssize_t mtd_flags_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
248 struct mtd_info *mtd = dev_get_drvdata(dev);
250 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
253 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
255 static ssize_t mtd_size_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct mtd_info *mtd = dev_get_drvdata(dev);
260 return snprintf(buf, PAGE_SIZE, "%llu\n",
261 (unsigned long long)mtd->size);
264 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
266 static ssize_t mtd_erasesize_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct mtd_info *mtd = dev_get_drvdata(dev);
271 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
274 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
276 static ssize_t mtd_writesize_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
279 struct mtd_info *mtd = dev_get_drvdata(dev);
281 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
284 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
286 static ssize_t mtd_subpagesize_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct mtd_info *mtd = dev_get_drvdata(dev);
290 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
292 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
295 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
297 static ssize_t mtd_oobsize_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct mtd_info *mtd = dev_get_drvdata(dev);
302 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
305 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
307 static ssize_t mtd_numeraseregions_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct mtd_info *mtd = dev_get_drvdata(dev);
312 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
315 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
318 static ssize_t mtd_name_show(struct device *dev,
319 struct device_attribute *attr, char *buf)
321 struct mtd_info *mtd = dev_get_drvdata(dev);
323 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
326 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
328 static ssize_t mtd_ecc_strength_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
331 struct mtd_info *mtd = dev_get_drvdata(dev);
333 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
335 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
337 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
338 struct device_attribute *attr,
341 struct mtd_info *mtd = dev_get_drvdata(dev);
343 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
346 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
350 struct mtd_info *mtd = dev_get_drvdata(dev);
351 unsigned int bitflip_threshold;
354 retval = kstrtouint(buf, 0, &bitflip_threshold);
358 mtd->bitflip_threshold = bitflip_threshold;
361 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
362 mtd_bitflip_threshold_show,
363 mtd_bitflip_threshold_store);
365 static ssize_t mtd_ecc_step_size_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct mtd_info *mtd = dev_get_drvdata(dev);
370 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
373 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
375 static struct attribute *mtd_attrs[] = {
377 &dev_attr_flags.attr,
379 &dev_attr_erasesize.attr,
380 &dev_attr_writesize.attr,
381 &dev_attr_subpagesize.attr,
382 &dev_attr_oobsize.attr,
383 &dev_attr_numeraseregions.attr,
385 &dev_attr_ecc_strength.attr,
386 &dev_attr_ecc_step_size.attr,
387 &dev_attr_bitflip_threshold.attr,
390 ATTRIBUTE_GROUPS(mtd);
392 static struct device_type mtd_devtype = {
394 .groups = mtd_groups,
395 .release = mtd_release,
400 * add_mtd_device - register an MTD device
401 * @mtd: pointer to new MTD device info structure
403 * Add a device to the list of MTD devices present in the system, and
404 * notify each currently active MTD 'user' of its arrival. Returns
405 * zero on success or 1 on failure, which currently will only happen
406 * if there is insufficient memory or a sysfs error.
409 int add_mtd_device(struct mtd_info *mtd)
412 struct mtd_notifier *not;
417 if (!mtd->backing_dev_info) {
420 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
423 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
426 mtd->backing_dev_info = &mtd_bdi_unmappable;
432 BUG_ON(mtd->writesize == 0);
433 mutex_lock(&mtd_table_mutex);
435 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
442 INIT_LIST_HEAD(&mtd->partitions);
444 /* default value if not set by driver */
445 if (mtd->bitflip_threshold == 0)
446 mtd->bitflip_threshold = mtd->ecc_strength;
448 if (is_power_of_2(mtd->erasesize))
449 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
451 mtd->erasesize_shift = 0;
453 if (is_power_of_2(mtd->writesize))
454 mtd->writesize_shift = ffs(mtd->writesize) - 1;
456 mtd->writesize_shift = 0;
458 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
459 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
461 /* Some chips always power up locked. Unlock them now */
462 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
463 error = mtd_unlock(mtd, 0, mtd->size);
464 if (error && error != -EOPNOTSUPP)
466 "%s: unlock failed, writes may not work\n",
471 /* Caller should have set dev.parent to match the
474 mtd->dev.type = &mtd_devtype;
475 mtd->dev.class = &mtd_class;
476 mtd->dev.devt = MTD_DEVT(i);
477 dev_set_name(&mtd->dev, "mtd%d", i);
478 dev_set_drvdata(&mtd->dev, mtd);
479 if (device_register(&mtd->dev) != 0)
483 device_create(&mtd_class, mtd->dev.parent,
487 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
488 /* No need to get a refcount on the module containing
489 the notifier, since we hold the mtd_table_mutex */
490 list_for_each_entry(not, &mtd_notifiers, list)
493 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
496 mutex_unlock(&mtd_table_mutex);
497 /* We _know_ we aren't being removed, because
498 our caller is still holding us here. So none
499 of this try_ nonsense, and no bitching about it
501 __module_get(THIS_MODULE);
506 idr_remove(&mtd_idr, i);
509 mutex_unlock(&mtd_table_mutex);
514 * del_mtd_device - unregister an MTD device
515 * @mtd: pointer to MTD device info structure
517 * Remove a device from the list of MTD devices present in the system,
518 * and notify each currently active MTD 'user' of its departure.
519 * Returns zero on success or 1 on failure, which currently will happen
520 * if the requested device does not appear to be present in the list.
523 int del_mtd_device(struct mtd_info *mtd)
527 struct mtd_notifier *not;
530 ret = del_mtd_partitions(mtd);
532 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
537 mutex_lock(&mtd_table_mutex);
539 if (idr_find(&mtd_idr, mtd->index) != mtd) {
545 /* No need to get a refcount on the module containing
546 the notifier, since we hold the mtd_table_mutex */
547 list_for_each_entry(not, &mtd_notifiers, list)
552 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
553 mtd->index, mtd->name, mtd->usecount);
557 device_unregister(&mtd->dev);
560 idr_remove(&mtd_idr, mtd->index);
562 module_put(THIS_MODULE);
567 mutex_unlock(&mtd_table_mutex);
573 * mtd_device_parse_register - parse partitions and register an MTD device.
575 * @mtd: the MTD device to register
576 * @types: the list of MTD partition probes to try, see
577 * 'parse_mtd_partitions()' for more information
578 * @parser_data: MTD partition parser-specific data
579 * @parts: fallback partition information to register, if parsing fails;
580 * only valid if %nr_parts > %0
581 * @nr_parts: the number of partitions in parts, if zero then the full
582 * MTD device is registered if no partition info is found
584 * This function aggregates MTD partitions parsing (done by
585 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
586 * basically follows the most common pattern found in many MTD drivers:
588 * * It first tries to probe partitions on MTD device @mtd using parsers
589 * specified in @types (if @types is %NULL, then the default list of parsers
590 * is used, see 'parse_mtd_partitions()' for more information). If none are
591 * found this functions tries to fallback to information specified in
593 * * If any partitioning info was found, this function registers the found
595 * * If no partitions were found this function just registers the MTD device
598 * Returns zero in case of success and a negative error code in case of failure.
600 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
601 struct mtd_part_parser_data *parser_data,
602 const struct mtd_partition *parts,
606 struct mtd_partition *real_parts;
608 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
609 if (err <= 0 && nr_parts && parts) {
610 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
619 err = add_mtd_partitions(mtd, real_parts, err);
621 } else if (err == 0) {
622 err = add_mtd_device(mtd);
629 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
632 * mtd_device_unregister - unregister an existing MTD device.
634 * @master: the MTD device to unregister. This will unregister both the master
635 * and any partitions if registered.
637 int mtd_device_unregister(struct mtd_info *master)
641 err = del_mtd_partitions(master);
645 if (!device_is_registered(&master->dev))
648 return del_mtd_device(master);
650 EXPORT_SYMBOL_GPL(mtd_device_unregister);
653 * register_mtd_user - register a 'user' of MTD devices.
654 * @new: pointer to notifier info structure
656 * Registers a pair of callbacks function to be called upon addition
657 * or removal of MTD devices. Causes the 'add' callback to be immediately
658 * invoked for each MTD device currently present in the system.
660 void register_mtd_user (struct mtd_notifier *new)
662 struct mtd_info *mtd;
664 mutex_lock(&mtd_table_mutex);
666 list_add(&new->list, &mtd_notifiers);
668 __module_get(THIS_MODULE);
670 mtd_for_each_device(mtd)
673 mutex_unlock(&mtd_table_mutex);
675 EXPORT_SYMBOL_GPL(register_mtd_user);
678 * unregister_mtd_user - unregister a 'user' of MTD devices.
679 * @old: pointer to notifier info structure
681 * Removes a callback function pair from the list of 'users' to be
682 * notified upon addition or removal of MTD devices. Causes the
683 * 'remove' callback to be immediately invoked for each MTD device
684 * currently present in the system.
686 int unregister_mtd_user (struct mtd_notifier *old)
688 struct mtd_info *mtd;
690 mutex_lock(&mtd_table_mutex);
692 module_put(THIS_MODULE);
694 mtd_for_each_device(mtd)
697 list_del(&old->list);
698 mutex_unlock(&mtd_table_mutex);
701 EXPORT_SYMBOL_GPL(unregister_mtd_user);
705 * get_mtd_device - obtain a validated handle for an MTD device
706 * @mtd: last known address of the required MTD device
707 * @num: internal device number of the required MTD device
709 * Given a number and NULL address, return the num'th entry in the device
710 * table, if any. Given an address and num == -1, search the device table
711 * for a device with that address and return if it's still present. Given
712 * both, return the num'th driver only if its address matches. Return
715 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
717 struct mtd_info *ret = NULL, *other;
720 mutex_lock(&mtd_table_mutex);
723 mtd_for_each_device(other) {
729 } else if (num >= 0) {
730 ret = idr_find(&mtd_idr, num);
731 if (mtd && mtd != ret)
740 err = __get_mtd_device(ret);
744 mutex_unlock(&mtd_table_mutex);
747 EXPORT_SYMBOL_GPL(get_mtd_device);
749 int __get_mtd_device(struct mtd_info *mtd)
753 if (!try_module_get(mtd->owner))
756 if (mtd->_get_device) {
757 err = mtd->_get_device(mtd);
760 module_put(mtd->owner);
767 EXPORT_SYMBOL_GPL(__get_mtd_device);
769 #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
770 static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
772 struct udevice *dev = NULL;
776 * If the first character of mtd name is '/', try interpreting as OF
777 * path. Otherwise try comparing by mtd->name and mtd->dev->name.
780 device_get_global_by_ofnode(ofnode_path(name), &dev);
782 is_part = mtd_is_partition(mtd);
784 return (!is_part && dev && mtd->dev == dev) ||
785 !strcmp(name, mtd->name) ||
786 (is_part && mtd->dev && !strcmp(name, mtd->dev->name));
789 static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
791 return !strcmp(name, mtd->name);
796 * get_mtd_device_nm - obtain a validated handle for an MTD device by
798 * @name: MTD device name to open
800 * This function returns MTD device description structure in case of
801 * success and an error code in case of failure.
803 struct mtd_info *get_mtd_device_nm(const char *name)
806 struct mtd_info *mtd = NULL, *other;
808 mutex_lock(&mtd_table_mutex);
810 mtd_for_each_device(other) {
812 if (mtd_device_matches_name(other, name)) {
814 printf("\nWarning: MTD name \"%s\" is not unique!\n\n",
818 #else /* !__UBOOT__ */
819 if (!strcmp(name, other->name)) {
823 #endif /* !__UBOOT__ */
829 err = __get_mtd_device(mtd);
833 mutex_unlock(&mtd_table_mutex);
837 mutex_unlock(&mtd_table_mutex);
840 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
842 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
844 * mtd_get_len_incl_bad
846 * Check if length including bad blocks fits into device.
848 * @param mtd an MTD device
849 * @param offset offset in flash
850 * @param length image length
851 * Return: image length including bad blocks in *len_incl_bad and whether or not
852 * the length returned was truncated in *truncated
854 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
855 const uint64_t length, uint64_t *len_incl_bad,
861 if (!mtd->_block_isbad) {
862 *len_incl_bad = length;
866 uint64_t len_excl_bad = 0;
869 while (len_excl_bad < length) {
870 if (offset >= mtd->size) {
875 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
877 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
878 len_excl_bad += block_len;
880 *len_incl_bad += block_len;
884 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
886 void put_mtd_device(struct mtd_info *mtd)
888 mutex_lock(&mtd_table_mutex);
889 __put_mtd_device(mtd);
890 mutex_unlock(&mtd_table_mutex);
893 EXPORT_SYMBOL_GPL(put_mtd_device);
895 void __put_mtd_device(struct mtd_info *mtd)
898 BUG_ON(mtd->usecount < 0);
900 if (mtd->_put_device)
901 mtd->_put_device(mtd);
903 module_put(mtd->owner);
905 EXPORT_SYMBOL_GPL(__put_mtd_device);
907 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
909 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
911 if (!(mtd->flags & MTD_WRITEABLE))
913 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
915 instr->state = MTD_ERASE_DONE;
918 return mtd->_erase(mtd, instr);
920 EXPORT_SYMBOL_GPL(mtd_erase);
924 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
926 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
927 void **virt, resource_size_t *phys)
935 if (from < 0 || from > mtd->size || len > mtd->size - from)
939 return mtd->_point(mtd, from, len, retlen, virt, phys);
941 EXPORT_SYMBOL_GPL(mtd_point);
943 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
944 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
948 if (from < 0 || from > mtd->size || len > mtd->size - from)
952 return mtd->_unpoint(mtd, from, len);
954 EXPORT_SYMBOL_GPL(mtd_unpoint);
958 * Allow NOMMU mmap() to directly map the device (if not NULL)
959 * - return the address to which the offset maps
960 * - return -ENOSYS to indicate refusal to do the mapping
962 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
963 unsigned long offset, unsigned long flags)
965 if (!mtd->_get_unmapped_area)
967 if (offset > mtd->size || len > mtd->size - offset)
969 return mtd->_get_unmapped_area(mtd, len, offset, flags);
971 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
973 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
978 if (from < 0 || from > mtd->size || len > mtd->size - from)
984 * In the absence of an error, drivers return a non-negative integer
985 * representing the maximum number of bitflips that were corrected on
986 * any one ecc region (if applicable; zero otherwise).
989 ret_code = mtd->_read(mtd, from, len, retlen, buf);
990 } else if (mtd->_read_oob) {
991 struct mtd_oob_ops ops = {
996 ret_code = mtd->_read_oob(mtd, from, &ops);
997 *retlen = ops.retlen;
1002 if (unlikely(ret_code < 0))
1004 if (mtd->ecc_strength == 0)
1005 return 0; /* device lacks ecc */
1006 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1008 EXPORT_SYMBOL_GPL(mtd_read);
1010 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1014 if (to < 0 || to > mtd->size || len > mtd->size - to)
1016 if ((!mtd->_write && !mtd->_write_oob) ||
1017 !(mtd->flags & MTD_WRITEABLE))
1023 struct mtd_oob_ops ops = {
1025 .datbuf = (u8 *)buf,
1029 ret = mtd->_write_oob(mtd, to, &ops);
1030 *retlen = ops.retlen;
1034 return mtd->_write(mtd, to, len, retlen, buf);
1036 EXPORT_SYMBOL_GPL(mtd_write);
1039 * In blackbox flight recorder like scenarios we want to make successful writes
1040 * in interrupt context. panic_write() is only intended to be called when its
1041 * known the kernel is about to panic and we need the write to succeed. Since
1042 * the kernel is not going to be running for much longer, this function can
1043 * break locks and delay to ensure the write succeeds (but not sleep).
1045 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1049 if (!mtd->_panic_write)
1051 if (to < 0 || to > mtd->size || len > mtd->size - to)
1053 if (!(mtd->flags & MTD_WRITEABLE))
1057 return mtd->_panic_write(mtd, to, len, retlen, buf);
1059 EXPORT_SYMBOL_GPL(mtd_panic_write);
1061 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1062 struct mtd_oob_ops *ops)
1065 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1066 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1075 if (offs < 0 || offs + ops->len > mtd->size)
1081 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1084 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1085 mtd_div_by_ws(offs, mtd)) *
1086 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1087 if (ops->ooblen > maxooblen)
1094 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1097 ops->retlen = ops->oobretlen = 0;
1099 ret_code = mtd_check_oob_ops(mtd, from, ops);
1103 /* Check the validity of a potential fallback on mtd->_read */
1104 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1108 ret_code = mtd->_read_oob(mtd, from, ops);
1110 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1114 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1115 * similar to mtd->_read(), returning a non-negative integer
1116 * representing max bitflips. In other cases, mtd->_read_oob() may
1117 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1119 if (unlikely(ret_code < 0))
1121 if (mtd->ecc_strength == 0)
1122 return 0; /* device lacks ecc */
1123 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1125 EXPORT_SYMBOL_GPL(mtd_read_oob);
1127 /* This is a bare copy of mtd_read_oob returning the actual number of bitflips */
1128 int mtd_read_oob_bf(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1131 ops->retlen = ops->oobretlen = 0;
1132 if (!mtd->_read_oob)
1135 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1136 * similar to mtd->_read(), returning a non-negative integer
1137 * representing max bitflips. In other cases, mtd->_read_oob() may
1138 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1140 ret_code = mtd->_read_oob(mtd, from, ops);
1141 if (unlikely(ret_code < 0))
1143 if (mtd->ecc_strength == 0)
1144 return 0; /* device lacks ecc */
1147 EXPORT_SYMBOL_GPL(mtd_read_oob_bf);
1149 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1150 struct mtd_oob_ops *ops)
1154 ops->retlen = ops->oobretlen = 0;
1156 if (!(mtd->flags & MTD_WRITEABLE))
1159 ret = mtd_check_oob_ops(mtd, to, ops);
1163 /* Check the validity of a potential fallback on mtd->_write */
1164 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1167 if (mtd->_write_oob)
1168 return mtd->_write_oob(mtd, to, ops);
1170 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1173 EXPORT_SYMBOL_GPL(mtd_write_oob);
1176 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1177 * @mtd: MTD device structure
1178 * @section: ECC section. Depending on the layout you may have all the ECC
1179 * bytes stored in a single contiguous section, or one section
1180 * per ECC chunk (and sometime several sections for a single ECC
1182 * @oobecc: OOB region struct filled with the appropriate ECC position
1185 * This function returns ECC section information in the OOB area. If you want
1186 * to get all the ECC bytes information, then you should call
1187 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1189 * Returns zero on success, a negative error code otherwise.
1191 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1192 struct mtd_oob_region *oobecc)
1194 memset(oobecc, 0, sizeof(*oobecc));
1196 if (!mtd || section < 0)
1199 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1202 return mtd->ooblayout->ecc(mtd, section, oobecc);
1204 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1207 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1209 * @mtd: MTD device structure
1210 * @section: Free section you are interested in. Depending on the layout
1211 * you may have all the free bytes stored in a single contiguous
1212 * section, or one section per ECC chunk plus an extra section
1213 * for the remaining bytes (or other funky layout).
1214 * @oobfree: OOB region struct filled with the appropriate free position
1217 * This function returns free bytes position in the OOB area. If you want
1218 * to get all the free bytes information, then you should call
1219 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1221 * Returns zero on success, a negative error code otherwise.
1223 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1224 struct mtd_oob_region *oobfree)
1226 memset(oobfree, 0, sizeof(*oobfree));
1228 if (!mtd || section < 0)
1231 if (!mtd->ooblayout || !mtd->ooblayout->rfree)
1234 return mtd->ooblayout->rfree(mtd, section, oobfree);
1236 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1239 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1240 * @mtd: mtd info structure
1241 * @byte: the byte we are searching for
1242 * @sectionp: pointer where the section id will be stored
1243 * @oobregion: used to retrieve the ECC position
1244 * @iter: iterator function. Should be either mtd_ooblayout_free or
1245 * mtd_ooblayout_ecc depending on the region type you're searching for
1247 * This function returns the section id and oobregion information of a
1248 * specific byte. For example, say you want to know where the 4th ECC byte is
1249 * stored, you'll use:
1251 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1253 * Returns zero on success, a negative error code otherwise.
1255 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1256 int *sectionp, struct mtd_oob_region *oobregion,
1257 int (*iter)(struct mtd_info *,
1259 struct mtd_oob_region *oobregion))
1261 int pos = 0, ret, section = 0;
1263 memset(oobregion, 0, sizeof(*oobregion));
1266 ret = iter(mtd, section, oobregion);
1270 if (pos + oobregion->length > byte)
1273 pos += oobregion->length;
1278 * Adjust region info to make it start at the beginning at the
1281 oobregion->offset += byte - pos;
1282 oobregion->length -= byte - pos;
1283 *sectionp = section;
1289 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1291 * @mtd: mtd info structure
1292 * @eccbyte: the byte we are searching for
1293 * @sectionp: pointer where the section id will be stored
1294 * @oobregion: OOB region information
1296 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1299 * Returns zero on success, a negative error code otherwise.
1301 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1303 struct mtd_oob_region *oobregion)
1305 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1308 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1311 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1312 * @mtd: mtd info structure
1313 * @buf: destination buffer to store OOB bytes
1314 * @oobbuf: OOB buffer
1315 * @start: first byte to retrieve
1316 * @nbytes: number of bytes to retrieve
1317 * @iter: section iterator
1319 * Extract bytes attached to a specific category (ECC or free)
1320 * from the OOB buffer and copy them into buf.
1322 * Returns zero on success, a negative error code otherwise.
1324 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1325 const u8 *oobbuf, int start, int nbytes,
1326 int (*iter)(struct mtd_info *,
1328 struct mtd_oob_region *oobregion))
1330 struct mtd_oob_region oobregion;
1333 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1339 cnt = min_t(int, nbytes, oobregion.length);
1340 memcpy(buf, oobbuf + oobregion.offset, cnt);
1347 ret = iter(mtd, ++section, &oobregion);
1354 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1355 * @mtd: mtd info structure
1356 * @buf: source buffer to get OOB bytes from
1357 * @oobbuf: OOB buffer
1358 * @start: first OOB byte to set
1359 * @nbytes: number of OOB bytes to set
1360 * @iter: section iterator
1362 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1363 * is selected by passing the appropriate iterator.
1365 * Returns zero on success, a negative error code otherwise.
1367 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1368 u8 *oobbuf, int start, int nbytes,
1369 int (*iter)(struct mtd_info *,
1371 struct mtd_oob_region *oobregion))
1373 struct mtd_oob_region oobregion;
1376 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1382 cnt = min_t(int, nbytes, oobregion.length);
1383 memcpy(oobbuf + oobregion.offset, buf, cnt);
1390 ret = iter(mtd, ++section, &oobregion);
1397 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1398 * @mtd: mtd info structure
1399 * @iter: category iterator
1401 * Count the number of bytes in a given category.
1403 * Returns a positive value on success, a negative error code otherwise.
1405 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1406 int (*iter)(struct mtd_info *,
1408 struct mtd_oob_region *oobregion))
1410 struct mtd_oob_region oobregion;
1411 int section = 0, ret, nbytes = 0;
1414 ret = iter(mtd, section++, &oobregion);
1421 nbytes += oobregion.length;
1428 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1429 * @mtd: mtd info structure
1430 * @eccbuf: destination buffer to store ECC bytes
1431 * @oobbuf: OOB buffer
1432 * @start: first ECC byte to retrieve
1433 * @nbytes: number of ECC bytes to retrieve
1435 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1437 * Returns zero on success, a negative error code otherwise.
1439 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1440 const u8 *oobbuf, int start, int nbytes)
1442 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1445 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1448 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1449 * @mtd: mtd info structure
1450 * @eccbuf: source buffer to get ECC bytes from
1451 * @oobbuf: OOB buffer
1452 * @start: first ECC byte to set
1453 * @nbytes: number of ECC bytes to set
1455 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1457 * Returns zero on success, a negative error code otherwise.
1459 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1460 u8 *oobbuf, int start, int nbytes)
1462 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1465 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1468 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1469 * @mtd: mtd info structure
1470 * @databuf: destination buffer to store ECC bytes
1471 * @oobbuf: OOB buffer
1472 * @start: first ECC byte to retrieve
1473 * @nbytes: number of ECC bytes to retrieve
1475 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1477 * Returns zero on success, a negative error code otherwise.
1479 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1480 const u8 *oobbuf, int start, int nbytes)
1482 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1483 mtd_ooblayout_free);
1485 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1488 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1489 * @mtd: mtd info structure
1490 * @eccbuf: source buffer to get data bytes from
1491 * @oobbuf: OOB buffer
1492 * @start: first ECC byte to set
1493 * @nbytes: number of ECC bytes to set
1495 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1497 * Returns zero on success, a negative error code otherwise.
1499 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1500 u8 *oobbuf, int start, int nbytes)
1502 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1503 mtd_ooblayout_free);
1505 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1508 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1509 * @mtd: mtd info structure
1511 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1513 * Returns zero on success, a negative error code otherwise.
1515 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1517 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1519 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1522 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1523 * @mtd: mtd info structure
1525 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1527 * Returns zero on success, a negative error code otherwise.
1529 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1531 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1533 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1536 * Method to access the protection register area, present in some flash
1537 * devices. The user data is one time programmable but the factory data is read
1540 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1541 struct otp_info *buf)
1543 if (!mtd->_get_fact_prot_info)
1547 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1549 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1551 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1552 size_t *retlen, u_char *buf)
1555 if (!mtd->_read_fact_prot_reg)
1559 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1561 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1563 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1564 struct otp_info *buf)
1566 if (!mtd->_get_user_prot_info)
1570 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1572 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1574 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1575 size_t *retlen, u_char *buf)
1578 if (!mtd->_read_user_prot_reg)
1582 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1584 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1586 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1587 size_t *retlen, u_char *buf)
1592 if (!mtd->_write_user_prot_reg)
1596 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1601 * If no data could be written at all, we are out of memory and
1602 * must return -ENOSPC.
1604 return (*retlen) ? 0 : -ENOSPC;
1606 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1608 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1610 if (!mtd->_lock_user_prot_reg)
1614 return mtd->_lock_user_prot_reg(mtd, from, len);
1616 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1618 /* Chip-supported device locking */
1619 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1623 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1627 return mtd->_lock(mtd, ofs, len);
1629 EXPORT_SYMBOL_GPL(mtd_lock);
1631 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1635 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1639 return mtd->_unlock(mtd, ofs, len);
1641 EXPORT_SYMBOL_GPL(mtd_unlock);
1643 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1645 if (!mtd->_is_locked)
1647 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1651 return mtd->_is_locked(mtd, ofs, len);
1653 EXPORT_SYMBOL_GPL(mtd_is_locked);
1655 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1657 if (ofs < 0 || ofs > mtd->size)
1659 if (!mtd->_block_isreserved)
1661 return mtd->_block_isreserved(mtd, ofs);
1663 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1665 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1667 if (ofs < 0 || ofs > mtd->size)
1669 if (!mtd->_block_isbad)
1671 return mtd->_block_isbad(mtd, ofs);
1673 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1675 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1677 if (!mtd->_block_markbad)
1679 if (ofs < 0 || ofs > mtd->size)
1681 if (!(mtd->flags & MTD_WRITEABLE))
1683 return mtd->_block_markbad(mtd, ofs);
1685 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1689 * default_mtd_writev - the default writev method
1690 * @mtd: mtd device description object pointer
1691 * @vecs: the vectors to write
1692 * @count: count of vectors in @vecs
1693 * @to: the MTD device offset to write to
1694 * @retlen: on exit contains the count of bytes written to the MTD device.
1696 * This function returns zero in case of success and a negative error code in
1699 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1700 unsigned long count, loff_t to, size_t *retlen)
1703 size_t totlen = 0, thislen;
1706 for (i = 0; i < count; i++) {
1707 if (!vecs[i].iov_len)
1709 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1712 if (ret || thislen != vecs[i].iov_len)
1714 to += vecs[i].iov_len;
1721 * mtd_writev - the vector-based MTD write method
1722 * @mtd: mtd device description object pointer
1723 * @vecs: the vectors to write
1724 * @count: count of vectors in @vecs
1725 * @to: the MTD device offset to write to
1726 * @retlen: on exit contains the count of bytes written to the MTD device.
1728 * This function returns zero in case of success and a negative error code in
1731 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1732 unsigned long count, loff_t to, size_t *retlen)
1735 if (!(mtd->flags & MTD_WRITEABLE))
1738 return default_mtd_writev(mtd, vecs, count, to, retlen);
1739 return mtd->_writev(mtd, vecs, count, to, retlen);
1741 EXPORT_SYMBOL_GPL(mtd_writev);
1744 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1745 * @mtd: mtd device description object pointer
1746 * @size: a pointer to the ideal or maximum size of the allocation, points
1747 * to the actual allocation size on success.
1749 * This routine attempts to allocate a contiguous kernel buffer up to
1750 * the specified size, backing off the size of the request exponentially
1751 * until the request succeeds or until the allocation size falls below
1752 * the system page size. This attempts to make sure it does not adversely
1753 * impact system performance, so when allocating more than one page, we
1754 * ask the memory allocator to avoid re-trying, swapping, writing back
1755 * or performing I/O.
1757 * Note, this function also makes sure that the allocated buffer is aligned to
1758 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1760 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1761 * to handle smaller (i.e. degraded) buffer allocations under low- or
1762 * fragmented-memory situations where such reduced allocations, from a
1763 * requested ideal, are allowed.
1765 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1767 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1769 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1770 __GFP_NORETRY | __GFP_NO_KSWAPD;
1771 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1774 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1776 while (*size > min_alloc) {
1777 kbuf = kmalloc(*size, flags);
1782 *size = ALIGN(*size, mtd->writesize);
1786 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1787 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1789 return kmalloc(*size, GFP_KERNEL);
1791 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1794 #ifdef CONFIG_PROC_FS
1796 /*====================================================================*/
1797 /* Support for /proc/mtd */
1799 static int mtd_proc_show(struct seq_file *m, void *v)
1801 struct mtd_info *mtd;
1803 seq_puts(m, "dev: size erasesize name\n");
1804 mutex_lock(&mtd_table_mutex);
1805 mtd_for_each_device(mtd) {
1806 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1807 mtd->index, (unsigned long long)mtd->size,
1808 mtd->erasesize, mtd->name);
1810 mutex_unlock(&mtd_table_mutex);
1814 static int mtd_proc_open(struct inode *inode, struct file *file)
1816 return single_open(file, mtd_proc_show, NULL);
1819 static const struct file_operations mtd_proc_ops = {
1820 .open = mtd_proc_open,
1822 .llseek = seq_lseek,
1823 .release = single_release,
1825 #endif /* CONFIG_PROC_FS */
1827 /*====================================================================*/
1831 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1835 ret = bdi_init(bdi);
1837 ret = bdi_register(bdi, NULL, "%s", name);
1845 static struct proc_dir_entry *proc_mtd;
1847 static int __init init_mtd(void)
1851 ret = class_register(&mtd_class);
1855 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1859 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1863 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1867 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1869 ret = init_mtdchar();
1877 remove_proc_entry("mtd", NULL);
1879 bdi_destroy(&mtd_bdi_ro_mappable);
1881 bdi_destroy(&mtd_bdi_unmappable);
1883 class_unregister(&mtd_class);
1885 pr_err("Error registering mtd class or bdi: %d\n", ret);
1889 static void __exit cleanup_mtd(void)
1893 remove_proc_entry("mtd", NULL);
1894 class_unregister(&mtd_class);
1895 bdi_destroy(&mtd_bdi_unmappable);
1896 bdi_destroy(&mtd_bdi_ro_mappable);
1897 bdi_destroy(&mtd_bdi_rw_mappable);
1900 module_init(init_mtd);
1901 module_exit(cleanup_mtd);
1904 MODULE_LICENSE("GPL");
1906 MODULE_DESCRIPTION("Core MTD registration and access routines");