1 // SPDX-License-Identifier: GPL-2.0+
3 * Core registration and callback routines for MTD
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7 * Copyright © 2006 Red Hat UK Limited
13 #include <dm/devres.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/ptrace.h>
17 #include <linux/seq_file.h>
18 #include <linux/string.h>
19 #include <linux/timer.h>
20 #include <linux/major.h>
22 #include <linux/err.h>
23 #include <linux/ioctl.h>
24 #include <linux/init.h>
25 #include <linux/proc_fs.h>
26 #include <linux/idr.h>
27 #include <linux/backing-dev.h>
28 #include <linux/gfp.h>
29 #include <linux/slab.h>
31 #include <linux/bitops.h>
32 #include <linux/bug.h>
33 #include <linux/err.h>
34 #include <ubi_uboot.h>
37 #include <linux/log2.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/partitions.h>
45 * backing device capabilities for non-mappable devices (such as NAND flash)
46 * - permits private mappings, copies are taken of the data
48 static struct backing_dev_info mtd_bdi_unmappable = {
49 .capabilities = BDI_CAP_MAP_COPY,
53 * backing device capabilities for R/O mappable devices (such as ROM)
54 * - permits private mappings, copies are taken of the data
55 * - permits non-writable shared mappings
57 static struct backing_dev_info mtd_bdi_ro_mappable = {
58 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
59 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
63 * backing device capabilities for writable mappable devices (such as RAM)
64 * - permits private mappings, copies are taken of the data
65 * - permits non-writable shared mappings
67 static struct backing_dev_info mtd_bdi_rw_mappable = {
68 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
69 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
73 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
74 static int mtd_cls_resume(struct device *dev);
76 static struct class mtd_class = {
79 .suspend = mtd_cls_suspend,
80 .resume = mtd_cls_resume,
91 struct idr_layer id[MAX_IDR_ID];
95 #define DEFINE_IDR(name) struct idr name;
97 void idr_remove(struct idr *idp, int id)
99 if (idp->id[id].used) {
100 idp->id[id].used = 0;
106 void *idr_find(struct idr *idp, int id)
108 if (idp->id[id].used)
109 return idp->id[id].ptr;
114 void *idr_get_next(struct idr *idp, int *next)
119 ret = idr_find(idp, id);
122 if (!idp->id[id].used)
132 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
134 struct idr_layer *idl;
137 while (i < MAX_IDR_ID) {
139 if (idl->used == 0) {
151 static DEFINE_IDR(mtd_idr);
153 /* These are exported solely for the purpose of mtd_blkdevs.c. You
154 should not use them for _anything_ else */
155 DEFINE_MUTEX(mtd_table_mutex);
156 EXPORT_SYMBOL_GPL(mtd_table_mutex);
158 struct mtd_info *__mtd_next_device(int i)
160 return idr_get_next(&mtd_idr, &i);
162 EXPORT_SYMBOL_GPL(__mtd_next_device);
164 bool mtd_dev_list_updated(void)
166 if (mtd_idr.updated) {
167 mtd_idr.updated = false;
175 static LIST_HEAD(mtd_notifiers);
178 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
180 /* REVISIT once MTD uses the driver model better, whoever allocates
181 * the mtd_info will probably want to use the release() hook...
183 static void mtd_release(struct device *dev)
185 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
186 dev_t index = MTD_DEVT(mtd->index);
188 /* remove /dev/mtdXro node if needed */
190 device_destroy(&mtd_class, index + 1);
193 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
195 struct mtd_info *mtd = dev_get_drvdata(dev);
197 return mtd ? mtd_suspend(mtd) : 0;
200 static int mtd_cls_resume(struct device *dev)
202 struct mtd_info *mtd = dev_get_drvdata(dev);
209 static ssize_t mtd_type_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct mtd_info *mtd = dev_get_drvdata(dev);
237 case MTD_MLCNANDFLASH:
244 return snprintf(buf, PAGE_SIZE, "%s\n", type);
246 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
248 static ssize_t mtd_flags_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct mtd_info *mtd = dev_get_drvdata(dev);
253 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
256 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
258 static ssize_t mtd_size_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
261 struct mtd_info *mtd = dev_get_drvdata(dev);
263 return snprintf(buf, PAGE_SIZE, "%llu\n",
264 (unsigned long long)mtd->size);
267 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
269 static ssize_t mtd_erasesize_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
272 struct mtd_info *mtd = dev_get_drvdata(dev);
274 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
277 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
279 static ssize_t mtd_writesize_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct mtd_info *mtd = dev_get_drvdata(dev);
284 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
287 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
289 static ssize_t mtd_subpagesize_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct mtd_info *mtd = dev_get_drvdata(dev);
293 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
295 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
298 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
300 static ssize_t mtd_oobsize_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct mtd_info *mtd = dev_get_drvdata(dev);
305 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
308 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
310 static ssize_t mtd_numeraseregions_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct mtd_info *mtd = dev_get_drvdata(dev);
315 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
318 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
321 static ssize_t mtd_name_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct mtd_info *mtd = dev_get_drvdata(dev);
326 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
329 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
331 static ssize_t mtd_ecc_strength_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 struct mtd_info *mtd = dev_get_drvdata(dev);
336 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
338 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
340 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
341 struct device_attribute *attr,
344 struct mtd_info *mtd = dev_get_drvdata(dev);
346 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
349 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
353 struct mtd_info *mtd = dev_get_drvdata(dev);
354 unsigned int bitflip_threshold;
357 retval = kstrtouint(buf, 0, &bitflip_threshold);
361 mtd->bitflip_threshold = bitflip_threshold;
364 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
365 mtd_bitflip_threshold_show,
366 mtd_bitflip_threshold_store);
368 static ssize_t mtd_ecc_step_size_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct mtd_info *mtd = dev_get_drvdata(dev);
373 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
376 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
378 static struct attribute *mtd_attrs[] = {
380 &dev_attr_flags.attr,
382 &dev_attr_erasesize.attr,
383 &dev_attr_writesize.attr,
384 &dev_attr_subpagesize.attr,
385 &dev_attr_oobsize.attr,
386 &dev_attr_numeraseregions.attr,
388 &dev_attr_ecc_strength.attr,
389 &dev_attr_ecc_step_size.attr,
390 &dev_attr_bitflip_threshold.attr,
393 ATTRIBUTE_GROUPS(mtd);
395 static struct device_type mtd_devtype = {
397 .groups = mtd_groups,
398 .release = mtd_release,
403 * add_mtd_device - register an MTD device
404 * @mtd: pointer to new MTD device info structure
406 * Add a device to the list of MTD devices present in the system, and
407 * notify each currently active MTD 'user' of its arrival. Returns
408 * zero on success or 1 on failure, which currently will only happen
409 * if there is insufficient memory or a sysfs error.
412 int add_mtd_device(struct mtd_info *mtd)
415 struct mtd_notifier *not;
420 if (!mtd->backing_dev_info) {
423 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
426 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
429 mtd->backing_dev_info = &mtd_bdi_unmappable;
435 BUG_ON(mtd->writesize == 0);
436 mutex_lock(&mtd_table_mutex);
438 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
445 INIT_LIST_HEAD(&mtd->partitions);
447 /* default value if not set by driver */
448 if (mtd->bitflip_threshold == 0)
449 mtd->bitflip_threshold = mtd->ecc_strength;
451 if (is_power_of_2(mtd->erasesize))
452 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
454 mtd->erasesize_shift = 0;
456 if (is_power_of_2(mtd->writesize))
457 mtd->writesize_shift = ffs(mtd->writesize) - 1;
459 mtd->writesize_shift = 0;
461 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
462 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
464 /* Some chips always power up locked. Unlock them now */
465 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
466 error = mtd_unlock(mtd, 0, mtd->size);
467 if (error && error != -EOPNOTSUPP)
469 "%s: unlock failed, writes may not work\n",
474 /* Caller should have set dev.parent to match the
477 mtd->dev.type = &mtd_devtype;
478 mtd->dev.class = &mtd_class;
479 mtd->dev.devt = MTD_DEVT(i);
480 dev_set_name(&mtd->dev, "mtd%d", i);
481 dev_set_drvdata(&mtd->dev, mtd);
482 if (device_register(&mtd->dev) != 0)
486 device_create(&mtd_class, mtd->dev.parent,
490 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
491 /* No need to get a refcount on the module containing
492 the notifier, since we hold the mtd_table_mutex */
493 list_for_each_entry(not, &mtd_notifiers, list)
496 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
499 mutex_unlock(&mtd_table_mutex);
500 /* We _know_ we aren't being removed, because
501 our caller is still holding us here. So none
502 of this try_ nonsense, and no bitching about it
504 __module_get(THIS_MODULE);
509 idr_remove(&mtd_idr, i);
512 mutex_unlock(&mtd_table_mutex);
517 * del_mtd_device - unregister an MTD device
518 * @mtd: pointer to MTD device info structure
520 * Remove a device from the list of MTD devices present in the system,
521 * and notify each currently active MTD 'user' of its departure.
522 * Returns zero on success or 1 on failure, which currently will happen
523 * if the requested device does not appear to be present in the list.
526 int del_mtd_device(struct mtd_info *mtd)
530 struct mtd_notifier *not;
533 ret = del_mtd_partitions(mtd);
535 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
540 mutex_lock(&mtd_table_mutex);
542 if (idr_find(&mtd_idr, mtd->index) != mtd) {
548 /* No need to get a refcount on the module containing
549 the notifier, since we hold the mtd_table_mutex */
550 list_for_each_entry(not, &mtd_notifiers, list)
555 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
556 mtd->index, mtd->name, mtd->usecount);
560 device_unregister(&mtd->dev);
563 idr_remove(&mtd_idr, mtd->index);
565 module_put(THIS_MODULE);
570 mutex_unlock(&mtd_table_mutex);
576 * mtd_device_parse_register - parse partitions and register an MTD device.
578 * @mtd: the MTD device to register
579 * @types: the list of MTD partition probes to try, see
580 * 'parse_mtd_partitions()' for more information
581 * @parser_data: MTD partition parser-specific data
582 * @parts: fallback partition information to register, if parsing fails;
583 * only valid if %nr_parts > %0
584 * @nr_parts: the number of partitions in parts, if zero then the full
585 * MTD device is registered if no partition info is found
587 * This function aggregates MTD partitions parsing (done by
588 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
589 * basically follows the most common pattern found in many MTD drivers:
591 * * It first tries to probe partitions on MTD device @mtd using parsers
592 * specified in @types (if @types is %NULL, then the default list of parsers
593 * is used, see 'parse_mtd_partitions()' for more information). If none are
594 * found this functions tries to fallback to information specified in
596 * * If any partitioning info was found, this function registers the found
598 * * If no partitions were found this function just registers the MTD device
601 * Returns zero in case of success and a negative error code in case of failure.
603 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
604 struct mtd_part_parser_data *parser_data,
605 const struct mtd_partition *parts,
609 struct mtd_partition *real_parts;
611 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
612 if (err <= 0 && nr_parts && parts) {
613 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
622 err = add_mtd_partitions(mtd, real_parts, err);
624 } else if (err == 0) {
625 err = add_mtd_device(mtd);
632 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
635 * mtd_device_unregister - unregister an existing MTD device.
637 * @master: the MTD device to unregister. This will unregister both the master
638 * and any partitions if registered.
640 int mtd_device_unregister(struct mtd_info *master)
644 err = del_mtd_partitions(master);
648 if (!device_is_registered(&master->dev))
651 return del_mtd_device(master);
653 EXPORT_SYMBOL_GPL(mtd_device_unregister);
656 * register_mtd_user - register a 'user' of MTD devices.
657 * @new: pointer to notifier info structure
659 * Registers a pair of callbacks function to be called upon addition
660 * or removal of MTD devices. Causes the 'add' callback to be immediately
661 * invoked for each MTD device currently present in the system.
663 void register_mtd_user (struct mtd_notifier *new)
665 struct mtd_info *mtd;
667 mutex_lock(&mtd_table_mutex);
669 list_add(&new->list, &mtd_notifiers);
671 __module_get(THIS_MODULE);
673 mtd_for_each_device(mtd)
676 mutex_unlock(&mtd_table_mutex);
678 EXPORT_SYMBOL_GPL(register_mtd_user);
681 * unregister_mtd_user - unregister a 'user' of MTD devices.
682 * @old: pointer to notifier info structure
684 * Removes a callback function pair from the list of 'users' to be
685 * notified upon addition or removal of MTD devices. Causes the
686 * 'remove' callback to be immediately invoked for each MTD device
687 * currently present in the system.
689 int unregister_mtd_user (struct mtd_notifier *old)
691 struct mtd_info *mtd;
693 mutex_lock(&mtd_table_mutex);
695 module_put(THIS_MODULE);
697 mtd_for_each_device(mtd)
700 list_del(&old->list);
701 mutex_unlock(&mtd_table_mutex);
704 EXPORT_SYMBOL_GPL(unregister_mtd_user);
708 * get_mtd_device - obtain a validated handle for an MTD device
709 * @mtd: last known address of the required MTD device
710 * @num: internal device number of the required MTD device
712 * Given a number and NULL address, return the num'th entry in the device
713 * table, if any. Given an address and num == -1, search the device table
714 * for a device with that address and return if it's still present. Given
715 * both, return the num'th driver only if its address matches. Return
718 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
720 struct mtd_info *ret = NULL, *other;
723 mutex_lock(&mtd_table_mutex);
726 mtd_for_each_device(other) {
732 } else if (num >= 0) {
733 ret = idr_find(&mtd_idr, num);
734 if (mtd && mtd != ret)
743 err = __get_mtd_device(ret);
747 mutex_unlock(&mtd_table_mutex);
750 EXPORT_SYMBOL_GPL(get_mtd_device);
753 int __get_mtd_device(struct mtd_info *mtd)
757 if (!try_module_get(mtd->owner))
760 if (mtd->_get_device) {
761 err = mtd->_get_device(mtd);
764 module_put(mtd->owner);
771 EXPORT_SYMBOL_GPL(__get_mtd_device);
774 * get_mtd_device_nm - obtain a validated handle for an MTD device by
776 * @name: MTD device name to open
778 * This function returns MTD device description structure in case of
779 * success and an error code in case of failure.
781 struct mtd_info *get_mtd_device_nm(const char *name)
784 struct mtd_info *mtd = NULL, *other;
786 mutex_lock(&mtd_table_mutex);
788 mtd_for_each_device(other) {
789 if (!strcmp(name, other->name)) {
798 err = __get_mtd_device(mtd);
802 mutex_unlock(&mtd_table_mutex);
806 mutex_unlock(&mtd_table_mutex);
809 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
811 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
813 * mtd_get_len_incl_bad
815 * Check if length including bad blocks fits into device.
817 * @param mtd an MTD device
818 * @param offset offset in flash
819 * @param length image length
820 * @return image length including bad blocks in *len_incl_bad and whether or not
821 * the length returned was truncated in *truncated
823 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
824 const uint64_t length, uint64_t *len_incl_bad,
830 if (!mtd->_block_isbad) {
831 *len_incl_bad = length;
835 uint64_t len_excl_bad = 0;
838 while (len_excl_bad < length) {
839 if (offset >= mtd->size) {
844 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
846 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
847 len_excl_bad += block_len;
849 *len_incl_bad += block_len;
853 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
855 void put_mtd_device(struct mtd_info *mtd)
857 mutex_lock(&mtd_table_mutex);
858 __put_mtd_device(mtd);
859 mutex_unlock(&mtd_table_mutex);
862 EXPORT_SYMBOL_GPL(put_mtd_device);
864 void __put_mtd_device(struct mtd_info *mtd)
867 BUG_ON(mtd->usecount < 0);
869 if (mtd->_put_device)
870 mtd->_put_device(mtd);
872 module_put(mtd->owner);
874 EXPORT_SYMBOL_GPL(__put_mtd_device);
877 * Erase is an asynchronous operation. Device drivers are supposed
878 * to call instr->callback() whenever the operation completes, even
879 * if it completes with a failure.
880 * Callers are supposed to pass a callback function and wait for it
881 * to be called before writing to the block.
883 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
885 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
887 if (!(mtd->flags & MTD_WRITEABLE))
889 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
891 instr->state = MTD_ERASE_DONE;
892 mtd_erase_callback(instr);
895 return mtd->_erase(mtd, instr);
897 EXPORT_SYMBOL_GPL(mtd_erase);
901 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
903 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
904 void **virt, resource_size_t *phys)
912 if (from < 0 || from > mtd->size || len > mtd->size - from)
916 return mtd->_point(mtd, from, len, retlen, virt, phys);
918 EXPORT_SYMBOL_GPL(mtd_point);
920 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
921 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
925 if (from < 0 || from > mtd->size || len > mtd->size - from)
929 return mtd->_unpoint(mtd, from, len);
931 EXPORT_SYMBOL_GPL(mtd_unpoint);
935 * Allow NOMMU mmap() to directly map the device (if not NULL)
936 * - return the address to which the offset maps
937 * - return -ENOSYS to indicate refusal to do the mapping
939 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
940 unsigned long offset, unsigned long flags)
942 if (!mtd->_get_unmapped_area)
944 if (offset > mtd->size || len > mtd->size - offset)
946 return mtd->_get_unmapped_area(mtd, len, offset, flags);
948 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
950 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
955 if (from < 0 || from > mtd->size || len > mtd->size - from)
961 * In the absence of an error, drivers return a non-negative integer
962 * representing the maximum number of bitflips that were corrected on
963 * any one ecc region (if applicable; zero otherwise).
966 ret_code = mtd->_read(mtd, from, len, retlen, buf);
967 } else if (mtd->_read_oob) {
968 struct mtd_oob_ops ops = {
973 ret_code = mtd->_read_oob(mtd, from, &ops);
974 *retlen = ops.retlen;
979 if (unlikely(ret_code < 0))
981 if (mtd->ecc_strength == 0)
982 return 0; /* device lacks ecc */
983 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
985 EXPORT_SYMBOL_GPL(mtd_read);
987 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
991 if (to < 0 || to > mtd->size || len > mtd->size - to)
993 if ((!mtd->_write && !mtd->_write_oob) ||
994 !(mtd->flags & MTD_WRITEABLE))
1000 struct mtd_oob_ops ops = {
1002 .datbuf = (u8 *)buf,
1006 ret = mtd->_write_oob(mtd, to, &ops);
1007 *retlen = ops.retlen;
1011 return mtd->_write(mtd, to, len, retlen, buf);
1013 EXPORT_SYMBOL_GPL(mtd_write);
1016 * In blackbox flight recorder like scenarios we want to make successful writes
1017 * in interrupt context. panic_write() is only intended to be called when its
1018 * known the kernel is about to panic and we need the write to succeed. Since
1019 * the kernel is not going to be running for much longer, this function can
1020 * break locks and delay to ensure the write succeeds (but not sleep).
1022 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1026 if (!mtd->_panic_write)
1028 if (to < 0 || to > mtd->size || len > mtd->size - to)
1030 if (!(mtd->flags & MTD_WRITEABLE))
1034 return mtd->_panic_write(mtd, to, len, retlen, buf);
1036 EXPORT_SYMBOL_GPL(mtd_panic_write);
1038 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1039 struct mtd_oob_ops *ops)
1042 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1043 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1052 if (offs < 0 || offs + ops->len > mtd->size)
1058 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1061 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1062 mtd_div_by_ws(offs, mtd)) *
1063 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1064 if (ops->ooblen > maxooblen)
1071 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1074 ops->retlen = ops->oobretlen = 0;
1076 ret_code = mtd_check_oob_ops(mtd, from, ops);
1080 /* Check the validity of a potential fallback on mtd->_read */
1081 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1085 ret_code = mtd->_read_oob(mtd, from, ops);
1087 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1091 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1092 * similar to mtd->_read(), returning a non-negative integer
1093 * representing max bitflips. In other cases, mtd->_read_oob() may
1094 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1096 if (unlikely(ret_code < 0))
1098 if (mtd->ecc_strength == 0)
1099 return 0; /* device lacks ecc */
1100 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1102 EXPORT_SYMBOL_GPL(mtd_read_oob);
1104 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1105 struct mtd_oob_ops *ops)
1109 ops->retlen = ops->oobretlen = 0;
1111 if (!(mtd->flags & MTD_WRITEABLE))
1114 ret = mtd_check_oob_ops(mtd, to, ops);
1118 /* Check the validity of a potential fallback on mtd->_write */
1119 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1122 if (mtd->_write_oob)
1123 return mtd->_write_oob(mtd, to, ops);
1125 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1128 EXPORT_SYMBOL_GPL(mtd_write_oob);
1131 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1132 * @mtd: MTD device structure
1133 * @section: ECC section. Depending on the layout you may have all the ECC
1134 * bytes stored in a single contiguous section, or one section
1135 * per ECC chunk (and sometime several sections for a single ECC
1137 * @oobecc: OOB region struct filled with the appropriate ECC position
1140 * This function returns ECC section information in the OOB area. If you want
1141 * to get all the ECC bytes information, then you should call
1142 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1144 * Returns zero on success, a negative error code otherwise.
1146 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1147 struct mtd_oob_region *oobecc)
1149 memset(oobecc, 0, sizeof(*oobecc));
1151 if (!mtd || section < 0)
1154 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1157 return mtd->ooblayout->ecc(mtd, section, oobecc);
1159 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1162 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1164 * @mtd: MTD device structure
1165 * @section: Free section you are interested in. Depending on the layout
1166 * you may have all the free bytes stored in a single contiguous
1167 * section, or one section per ECC chunk plus an extra section
1168 * for the remaining bytes (or other funky layout).
1169 * @oobfree: OOB region struct filled with the appropriate free position
1172 * This function returns free bytes position in the OOB area. If you want
1173 * to get all the free bytes information, then you should call
1174 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1176 * Returns zero on success, a negative error code otherwise.
1178 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1179 struct mtd_oob_region *oobfree)
1181 memset(oobfree, 0, sizeof(*oobfree));
1183 if (!mtd || section < 0)
1186 if (!mtd->ooblayout || !mtd->ooblayout->rfree)
1189 return mtd->ooblayout->rfree(mtd, section, oobfree);
1191 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1194 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1195 * @mtd: mtd info structure
1196 * @byte: the byte we are searching for
1197 * @sectionp: pointer where the section id will be stored
1198 * @oobregion: used to retrieve the ECC position
1199 * @iter: iterator function. Should be either mtd_ooblayout_free or
1200 * mtd_ooblayout_ecc depending on the region type you're searching for
1202 * This function returns the section id and oobregion information of a
1203 * specific byte. For example, say you want to know where the 4th ECC byte is
1204 * stored, you'll use:
1206 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1208 * Returns zero on success, a negative error code otherwise.
1210 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1211 int *sectionp, struct mtd_oob_region *oobregion,
1212 int (*iter)(struct mtd_info *,
1214 struct mtd_oob_region *oobregion))
1216 int pos = 0, ret, section = 0;
1218 memset(oobregion, 0, sizeof(*oobregion));
1221 ret = iter(mtd, section, oobregion);
1225 if (pos + oobregion->length > byte)
1228 pos += oobregion->length;
1233 * Adjust region info to make it start at the beginning at the
1236 oobregion->offset += byte - pos;
1237 oobregion->length -= byte - pos;
1238 *sectionp = section;
1244 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1246 * @mtd: mtd info structure
1247 * @eccbyte: the byte we are searching for
1248 * @sectionp: pointer where the section id will be stored
1249 * @oobregion: OOB region information
1251 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1254 * Returns zero on success, a negative error code otherwise.
1256 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1258 struct mtd_oob_region *oobregion)
1260 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1263 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1266 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1267 * @mtd: mtd info structure
1268 * @buf: destination buffer to store OOB bytes
1269 * @oobbuf: OOB buffer
1270 * @start: first byte to retrieve
1271 * @nbytes: number of bytes to retrieve
1272 * @iter: section iterator
1274 * Extract bytes attached to a specific category (ECC or free)
1275 * from the OOB buffer and copy them into buf.
1277 * Returns zero on success, a negative error code otherwise.
1279 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1280 const u8 *oobbuf, int start, int nbytes,
1281 int (*iter)(struct mtd_info *,
1283 struct mtd_oob_region *oobregion))
1285 struct mtd_oob_region oobregion;
1288 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1294 cnt = min_t(int, nbytes, oobregion.length);
1295 memcpy(buf, oobbuf + oobregion.offset, cnt);
1302 ret = iter(mtd, ++section, &oobregion);
1309 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1310 * @mtd: mtd info structure
1311 * @buf: source buffer to get OOB bytes from
1312 * @oobbuf: OOB buffer
1313 * @start: first OOB byte to set
1314 * @nbytes: number of OOB bytes to set
1315 * @iter: section iterator
1317 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1318 * is selected by passing the appropriate iterator.
1320 * Returns zero on success, a negative error code otherwise.
1322 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1323 u8 *oobbuf, int start, int nbytes,
1324 int (*iter)(struct mtd_info *,
1326 struct mtd_oob_region *oobregion))
1328 struct mtd_oob_region oobregion;
1331 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1337 cnt = min_t(int, nbytes, oobregion.length);
1338 memcpy(oobbuf + oobregion.offset, buf, cnt);
1345 ret = iter(mtd, ++section, &oobregion);
1352 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1353 * @mtd: mtd info structure
1354 * @iter: category iterator
1356 * Count the number of bytes in a given category.
1358 * Returns a positive value on success, a negative error code otherwise.
1360 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1361 int (*iter)(struct mtd_info *,
1363 struct mtd_oob_region *oobregion))
1365 struct mtd_oob_region oobregion;
1366 int section = 0, ret, nbytes = 0;
1369 ret = iter(mtd, section++, &oobregion);
1376 nbytes += oobregion.length;
1383 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1384 * @mtd: mtd info structure
1385 * @eccbuf: destination buffer to store ECC bytes
1386 * @oobbuf: OOB buffer
1387 * @start: first ECC byte to retrieve
1388 * @nbytes: number of ECC bytes to retrieve
1390 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1392 * Returns zero on success, a negative error code otherwise.
1394 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1395 const u8 *oobbuf, int start, int nbytes)
1397 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1400 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1403 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1404 * @mtd: mtd info structure
1405 * @eccbuf: source buffer to get ECC bytes from
1406 * @oobbuf: OOB buffer
1407 * @start: first ECC byte to set
1408 * @nbytes: number of ECC bytes to set
1410 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1412 * Returns zero on success, a negative error code otherwise.
1414 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1415 u8 *oobbuf, int start, int nbytes)
1417 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1420 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1423 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1424 * @mtd: mtd info structure
1425 * @databuf: destination buffer to store ECC bytes
1426 * @oobbuf: OOB buffer
1427 * @start: first ECC byte to retrieve
1428 * @nbytes: number of ECC bytes to retrieve
1430 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1432 * Returns zero on success, a negative error code otherwise.
1434 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1435 const u8 *oobbuf, int start, int nbytes)
1437 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1438 mtd_ooblayout_free);
1440 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1443 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1444 * @mtd: mtd info structure
1445 * @eccbuf: source buffer to get data bytes from
1446 * @oobbuf: OOB buffer
1447 * @start: first ECC byte to set
1448 * @nbytes: number of ECC bytes to set
1450 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1452 * Returns zero on success, a negative error code otherwise.
1454 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1455 u8 *oobbuf, int start, int nbytes)
1457 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1458 mtd_ooblayout_free);
1460 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1463 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1464 * @mtd: mtd info structure
1466 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1468 * Returns zero on success, a negative error code otherwise.
1470 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1472 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1474 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1477 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1478 * @mtd: mtd info structure
1480 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1482 * Returns zero on success, a negative error code otherwise.
1484 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1486 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1488 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1491 * Method to access the protection register area, present in some flash
1492 * devices. The user data is one time programmable but the factory data is read
1495 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1496 struct otp_info *buf)
1498 if (!mtd->_get_fact_prot_info)
1502 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1504 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1506 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1507 size_t *retlen, u_char *buf)
1510 if (!mtd->_read_fact_prot_reg)
1514 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1516 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1518 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1519 struct otp_info *buf)
1521 if (!mtd->_get_user_prot_info)
1525 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1527 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1529 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1530 size_t *retlen, u_char *buf)
1533 if (!mtd->_read_user_prot_reg)
1537 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1539 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1541 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1542 size_t *retlen, u_char *buf)
1547 if (!mtd->_write_user_prot_reg)
1551 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1556 * If no data could be written at all, we are out of memory and
1557 * must return -ENOSPC.
1559 return (*retlen) ? 0 : -ENOSPC;
1561 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1563 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1565 if (!mtd->_lock_user_prot_reg)
1569 return mtd->_lock_user_prot_reg(mtd, from, len);
1571 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1573 /* Chip-supported device locking */
1574 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1578 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1582 return mtd->_lock(mtd, ofs, len);
1584 EXPORT_SYMBOL_GPL(mtd_lock);
1586 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1590 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1594 return mtd->_unlock(mtd, ofs, len);
1596 EXPORT_SYMBOL_GPL(mtd_unlock);
1598 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1600 if (!mtd->_is_locked)
1602 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1606 return mtd->_is_locked(mtd, ofs, len);
1608 EXPORT_SYMBOL_GPL(mtd_is_locked);
1610 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1612 if (ofs < 0 || ofs > mtd->size)
1614 if (!mtd->_block_isreserved)
1616 return mtd->_block_isreserved(mtd, ofs);
1618 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1620 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1622 if (ofs < 0 || ofs > mtd->size)
1624 if (!mtd->_block_isbad)
1626 return mtd->_block_isbad(mtd, ofs);
1628 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1630 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1632 if (!mtd->_block_markbad)
1634 if (ofs < 0 || ofs > mtd->size)
1636 if (!(mtd->flags & MTD_WRITEABLE))
1638 return mtd->_block_markbad(mtd, ofs);
1640 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1644 * default_mtd_writev - the default writev method
1645 * @mtd: mtd device description object pointer
1646 * @vecs: the vectors to write
1647 * @count: count of vectors in @vecs
1648 * @to: the MTD device offset to write to
1649 * @retlen: on exit contains the count of bytes written to the MTD device.
1651 * This function returns zero in case of success and a negative error code in
1654 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1655 unsigned long count, loff_t to, size_t *retlen)
1658 size_t totlen = 0, thislen;
1661 for (i = 0; i < count; i++) {
1662 if (!vecs[i].iov_len)
1664 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1667 if (ret || thislen != vecs[i].iov_len)
1669 to += vecs[i].iov_len;
1676 * mtd_writev - the vector-based MTD write method
1677 * @mtd: mtd device description object pointer
1678 * @vecs: the vectors to write
1679 * @count: count of vectors in @vecs
1680 * @to: the MTD device offset to write to
1681 * @retlen: on exit contains the count of bytes written to the MTD device.
1683 * This function returns zero in case of success and a negative error code in
1686 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1687 unsigned long count, loff_t to, size_t *retlen)
1690 if (!(mtd->flags & MTD_WRITEABLE))
1693 return default_mtd_writev(mtd, vecs, count, to, retlen);
1694 return mtd->_writev(mtd, vecs, count, to, retlen);
1696 EXPORT_SYMBOL_GPL(mtd_writev);
1699 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1700 * @mtd: mtd device description object pointer
1701 * @size: a pointer to the ideal or maximum size of the allocation, points
1702 * to the actual allocation size on success.
1704 * This routine attempts to allocate a contiguous kernel buffer up to
1705 * the specified size, backing off the size of the request exponentially
1706 * until the request succeeds or until the allocation size falls below
1707 * the system page size. This attempts to make sure it does not adversely
1708 * impact system performance, so when allocating more than one page, we
1709 * ask the memory allocator to avoid re-trying, swapping, writing back
1710 * or performing I/O.
1712 * Note, this function also makes sure that the allocated buffer is aligned to
1713 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1715 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1716 * to handle smaller (i.e. degraded) buffer allocations under low- or
1717 * fragmented-memory situations where such reduced allocations, from a
1718 * requested ideal, are allowed.
1720 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1722 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1724 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1725 __GFP_NORETRY | __GFP_NO_KSWAPD;
1726 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1729 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1731 while (*size > min_alloc) {
1732 kbuf = kmalloc(*size, flags);
1737 *size = ALIGN(*size, mtd->writesize);
1741 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1742 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1744 return kmalloc(*size, GFP_KERNEL);
1746 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1749 #ifdef CONFIG_PROC_FS
1751 /*====================================================================*/
1752 /* Support for /proc/mtd */
1754 static int mtd_proc_show(struct seq_file *m, void *v)
1756 struct mtd_info *mtd;
1758 seq_puts(m, "dev: size erasesize name\n");
1759 mutex_lock(&mtd_table_mutex);
1760 mtd_for_each_device(mtd) {
1761 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1762 mtd->index, (unsigned long long)mtd->size,
1763 mtd->erasesize, mtd->name);
1765 mutex_unlock(&mtd_table_mutex);
1769 static int mtd_proc_open(struct inode *inode, struct file *file)
1771 return single_open(file, mtd_proc_show, NULL);
1774 static const struct file_operations mtd_proc_ops = {
1775 .open = mtd_proc_open,
1777 .llseek = seq_lseek,
1778 .release = single_release,
1780 #endif /* CONFIG_PROC_FS */
1782 /*====================================================================*/
1786 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1790 ret = bdi_init(bdi);
1792 ret = bdi_register(bdi, NULL, "%s", name);
1800 static struct proc_dir_entry *proc_mtd;
1802 static int __init init_mtd(void)
1806 ret = class_register(&mtd_class);
1810 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1814 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1818 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1822 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1824 ret = init_mtdchar();
1832 remove_proc_entry("mtd", NULL);
1834 bdi_destroy(&mtd_bdi_ro_mappable);
1836 bdi_destroy(&mtd_bdi_unmappable);
1838 class_unregister(&mtd_class);
1840 pr_err("Error registering mtd class or bdi: %d\n", ret);
1844 static void __exit cleanup_mtd(void)
1848 remove_proc_entry("mtd", NULL);
1849 class_unregister(&mtd_class);
1850 bdi_destroy(&mtd_bdi_unmappable);
1851 bdi_destroy(&mtd_bdi_ro_mappable);
1852 bdi_destroy(&mtd_bdi_rw_mappable);
1855 module_init(init_mtd);
1856 module_exit(cleanup_mtd);
1859 MODULE_LICENSE("GPL");
1860 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1861 MODULE_DESCRIPTION("Core MTD registration and access routines");