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
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/err.h>
30 #include <ubi_uboot.h>
33 #include <linux/log2.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
41 * backing device capabilities for non-mappable devices (such as NAND flash)
42 * - permits private mappings, copies are taken of the data
44 static struct backing_dev_info mtd_bdi_unmappable = {
45 .capabilities = BDI_CAP_MAP_COPY,
49 * backing device capabilities for R/O mappable devices (such as ROM)
50 * - permits private mappings, copies are taken of the data
51 * - permits non-writable shared mappings
53 static struct backing_dev_info mtd_bdi_ro_mappable = {
54 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
55 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
59 * backing device capabilities for writable mappable devices (such as RAM)
60 * - permits private mappings, copies are taken of the data
61 * - permits non-writable shared mappings
63 static struct backing_dev_info mtd_bdi_rw_mappable = {
64 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
65 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
69 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
70 static int mtd_cls_resume(struct device *dev);
72 static struct class mtd_class = {
75 .suspend = mtd_cls_suspend,
76 .resume = mtd_cls_resume,
87 struct idr_layer id[MAX_IDR_ID];
91 #define DEFINE_IDR(name) struct idr name;
93 void idr_remove(struct idr *idp, int id)
95 if (idp->id[id].used) {
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) {
147 static DEFINE_IDR(mtd_idr);
149 /* These are exported solely for the purpose of mtd_blkdevs.c. You
150 should not use them for _anything_ else */
151 DEFINE_MUTEX(mtd_table_mutex);
152 EXPORT_SYMBOL_GPL(mtd_table_mutex);
154 struct mtd_info *__mtd_next_device(int i)
156 return idr_get_next(&mtd_idr, &i);
158 EXPORT_SYMBOL_GPL(__mtd_next_device);
160 bool mtd_dev_list_updated(void)
162 if (mtd_idr.updated) {
163 mtd_idr.updated = false;
171 static LIST_HEAD(mtd_notifiers);
174 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
176 /* REVISIT once MTD uses the driver model better, whoever allocates
177 * the mtd_info will probably want to use the release() hook...
179 static void mtd_release(struct device *dev)
181 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
182 dev_t index = MTD_DEVT(mtd->index);
184 /* remove /dev/mtdXro node if needed */
186 device_destroy(&mtd_class, index + 1);
189 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
191 struct mtd_info *mtd = dev_get_drvdata(dev);
193 return mtd ? mtd_suspend(mtd) : 0;
196 static int mtd_cls_resume(struct device *dev)
198 struct mtd_info *mtd = dev_get_drvdata(dev);
205 static ssize_t mtd_type_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
208 struct mtd_info *mtd = dev_get_drvdata(dev);
233 case MTD_MLCNANDFLASH:
240 return snprintf(buf, PAGE_SIZE, "%s\n", type);
242 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
244 static ssize_t mtd_flags_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct mtd_info *mtd = dev_get_drvdata(dev);
249 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
252 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
254 static ssize_t mtd_size_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, "%llu\n",
260 (unsigned long long)mtd->size);
263 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
265 static ssize_t mtd_erasesize_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
268 struct mtd_info *mtd = dev_get_drvdata(dev);
270 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
273 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
275 static ssize_t mtd_writesize_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct mtd_info *mtd = dev_get_drvdata(dev);
280 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
283 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
285 static ssize_t mtd_subpagesize_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct mtd_info *mtd = dev_get_drvdata(dev);
289 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
291 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
294 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
296 static ssize_t mtd_oobsize_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct mtd_info *mtd = dev_get_drvdata(dev);
301 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
304 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
306 static ssize_t mtd_numeraseregions_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, "%u\n", mtd->numeraseregions);
314 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
317 static ssize_t mtd_name_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 struct mtd_info *mtd = dev_get_drvdata(dev);
322 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
325 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
327 static ssize_t mtd_ecc_strength_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct mtd_info *mtd = dev_get_drvdata(dev);
332 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
334 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
336 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
337 struct device_attribute *attr,
340 struct mtd_info *mtd = dev_get_drvdata(dev);
342 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
345 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
349 struct mtd_info *mtd = dev_get_drvdata(dev);
350 unsigned int bitflip_threshold;
353 retval = kstrtouint(buf, 0, &bitflip_threshold);
357 mtd->bitflip_threshold = bitflip_threshold;
360 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
361 mtd_bitflip_threshold_show,
362 mtd_bitflip_threshold_store);
364 static ssize_t mtd_ecc_step_size_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct mtd_info *mtd = dev_get_drvdata(dev);
369 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
372 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
374 static struct attribute *mtd_attrs[] = {
376 &dev_attr_flags.attr,
378 &dev_attr_erasesize.attr,
379 &dev_attr_writesize.attr,
380 &dev_attr_subpagesize.attr,
381 &dev_attr_oobsize.attr,
382 &dev_attr_numeraseregions.attr,
384 &dev_attr_ecc_strength.attr,
385 &dev_attr_ecc_step_size.attr,
386 &dev_attr_bitflip_threshold.attr,
389 ATTRIBUTE_GROUPS(mtd);
391 static struct device_type mtd_devtype = {
393 .groups = mtd_groups,
394 .release = mtd_release,
399 * add_mtd_device - register an MTD device
400 * @mtd: pointer to new MTD device info structure
402 * Add a device to the list of MTD devices present in the system, and
403 * notify each currently active MTD 'user' of its arrival. Returns
404 * zero on success or 1 on failure, which currently will only happen
405 * if there is insufficient memory or a sysfs error.
408 int add_mtd_device(struct mtd_info *mtd)
411 struct mtd_notifier *not;
416 if (!mtd->backing_dev_info) {
419 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
422 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
425 mtd->backing_dev_info = &mtd_bdi_unmappable;
431 BUG_ON(mtd->writesize == 0);
432 mutex_lock(&mtd_table_mutex);
434 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
441 INIT_LIST_HEAD(&mtd->partitions);
443 /* default value if not set by driver */
444 if (mtd->bitflip_threshold == 0)
445 mtd->bitflip_threshold = mtd->ecc_strength;
447 if (is_power_of_2(mtd->erasesize))
448 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
450 mtd->erasesize_shift = 0;
452 if (is_power_of_2(mtd->writesize))
453 mtd->writesize_shift = ffs(mtd->writesize) - 1;
455 mtd->writesize_shift = 0;
457 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
458 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
460 /* Some chips always power up locked. Unlock them now */
461 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
462 error = mtd_unlock(mtd, 0, mtd->size);
463 if (error && error != -EOPNOTSUPP)
465 "%s: unlock failed, writes may not work\n",
470 /* Caller should have set dev.parent to match the
473 mtd->dev.type = &mtd_devtype;
474 mtd->dev.class = &mtd_class;
475 mtd->dev.devt = MTD_DEVT(i);
476 dev_set_name(&mtd->dev, "mtd%d", i);
477 dev_set_drvdata(&mtd->dev, mtd);
478 if (device_register(&mtd->dev) != 0)
482 device_create(&mtd_class, mtd->dev.parent,
486 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
487 /* No need to get a refcount on the module containing
488 the notifier, since we hold the mtd_table_mutex */
489 list_for_each_entry(not, &mtd_notifiers, list)
492 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
495 mutex_unlock(&mtd_table_mutex);
496 /* We _know_ we aren't being removed, because
497 our caller is still holding us here. So none
498 of this try_ nonsense, and no bitching about it
500 __module_get(THIS_MODULE);
505 idr_remove(&mtd_idr, i);
508 mutex_unlock(&mtd_table_mutex);
513 * del_mtd_device - unregister an MTD device
514 * @mtd: pointer to MTD device info structure
516 * Remove a device from the list of MTD devices present in the system,
517 * and notify each currently active MTD 'user' of its departure.
518 * Returns zero on success or 1 on failure, which currently will happen
519 * if the requested device does not appear to be present in the list.
522 int del_mtd_device(struct mtd_info *mtd)
526 struct mtd_notifier *not;
529 ret = del_mtd_partitions(mtd);
531 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
536 mutex_lock(&mtd_table_mutex);
538 if (idr_find(&mtd_idr, mtd->index) != mtd) {
544 /* No need to get a refcount on the module containing
545 the notifier, since we hold the mtd_table_mutex */
546 list_for_each_entry(not, &mtd_notifiers, list)
551 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
552 mtd->index, mtd->name, mtd->usecount);
556 device_unregister(&mtd->dev);
559 idr_remove(&mtd_idr, mtd->index);
561 module_put(THIS_MODULE);
566 mutex_unlock(&mtd_table_mutex);
572 * mtd_device_parse_register - parse partitions and register an MTD device.
574 * @mtd: the MTD device to register
575 * @types: the list of MTD partition probes to try, see
576 * 'parse_mtd_partitions()' for more information
577 * @parser_data: MTD partition parser-specific data
578 * @parts: fallback partition information to register, if parsing fails;
579 * only valid if %nr_parts > %0
580 * @nr_parts: the number of partitions in parts, if zero then the full
581 * MTD device is registered if no partition info is found
583 * This function aggregates MTD partitions parsing (done by
584 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
585 * basically follows the most common pattern found in many MTD drivers:
587 * * It first tries to probe partitions on MTD device @mtd using parsers
588 * specified in @types (if @types is %NULL, then the default list of parsers
589 * is used, see 'parse_mtd_partitions()' for more information). If none are
590 * found this functions tries to fallback to information specified in
592 * * If any partitioning info was found, this function registers the found
594 * * If no partitions were found this function just registers the MTD device
597 * Returns zero in case of success and a negative error code in case of failure.
599 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
600 struct mtd_part_parser_data *parser_data,
601 const struct mtd_partition *parts,
605 struct mtd_partition *real_parts;
607 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
608 if (err <= 0 && nr_parts && parts) {
609 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
618 err = add_mtd_partitions(mtd, real_parts, err);
620 } else if (err == 0) {
621 err = add_mtd_device(mtd);
628 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
631 * mtd_device_unregister - unregister an existing MTD device.
633 * @master: the MTD device to unregister. This will unregister both the master
634 * and any partitions if registered.
636 int mtd_device_unregister(struct mtd_info *master)
640 err = del_mtd_partitions(master);
644 if (!device_is_registered(&master->dev))
647 return del_mtd_device(master);
649 EXPORT_SYMBOL_GPL(mtd_device_unregister);
652 * register_mtd_user - register a 'user' of MTD devices.
653 * @new: pointer to notifier info structure
655 * Registers a pair of callbacks function to be called upon addition
656 * or removal of MTD devices. Causes the 'add' callback to be immediately
657 * invoked for each MTD device currently present in the system.
659 void register_mtd_user (struct mtd_notifier *new)
661 struct mtd_info *mtd;
663 mutex_lock(&mtd_table_mutex);
665 list_add(&new->list, &mtd_notifiers);
667 __module_get(THIS_MODULE);
669 mtd_for_each_device(mtd)
672 mutex_unlock(&mtd_table_mutex);
674 EXPORT_SYMBOL_GPL(register_mtd_user);
677 * unregister_mtd_user - unregister a 'user' of MTD devices.
678 * @old: pointer to notifier info structure
680 * Removes a callback function pair from the list of 'users' to be
681 * notified upon addition or removal of MTD devices. Causes the
682 * 'remove' callback to be immediately invoked for each MTD device
683 * currently present in the system.
685 int unregister_mtd_user (struct mtd_notifier *old)
687 struct mtd_info *mtd;
689 mutex_lock(&mtd_table_mutex);
691 module_put(THIS_MODULE);
693 mtd_for_each_device(mtd)
696 list_del(&old->list);
697 mutex_unlock(&mtd_table_mutex);
700 EXPORT_SYMBOL_GPL(unregister_mtd_user);
704 * get_mtd_device - obtain a validated handle for an MTD device
705 * @mtd: last known address of the required MTD device
706 * @num: internal device number of the required MTD device
708 * Given a number and NULL address, return the num'th entry in the device
709 * table, if any. Given an address and num == -1, search the device table
710 * for a device with that address and return if it's still present. Given
711 * both, return the num'th driver only if its address matches. Return
714 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
716 struct mtd_info *ret = NULL, *other;
719 mutex_lock(&mtd_table_mutex);
722 mtd_for_each_device(other) {
728 } else if (num >= 0) {
729 ret = idr_find(&mtd_idr, num);
730 if (mtd && mtd != ret)
739 err = __get_mtd_device(ret);
743 mutex_unlock(&mtd_table_mutex);
746 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);
770 * get_mtd_device_nm - obtain a validated handle for an MTD device by
772 * @name: MTD device name to open
774 * This function returns MTD device description structure in case of
775 * success and an error code in case of failure.
777 struct mtd_info *get_mtd_device_nm(const char *name)
780 struct mtd_info *mtd = NULL, *other;
782 mutex_lock(&mtd_table_mutex);
784 mtd_for_each_device(other) {
785 if (!strcmp(name, other->name)) {
794 err = __get_mtd_device(mtd);
798 mutex_unlock(&mtd_table_mutex);
802 mutex_unlock(&mtd_table_mutex);
805 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
807 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
809 * mtd_get_len_incl_bad
811 * Check if length including bad blocks fits into device.
813 * @param mtd an MTD device
814 * @param offset offset in flash
815 * @param length image length
816 * @return image length including bad blocks in *len_incl_bad and whether or not
817 * the length returned was truncated in *truncated
819 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
820 const uint64_t length, uint64_t *len_incl_bad,
826 if (!mtd->_block_isbad) {
827 *len_incl_bad = length;
831 uint64_t len_excl_bad = 0;
834 while (len_excl_bad < length) {
835 if (offset >= mtd->size) {
840 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
842 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
843 len_excl_bad += block_len;
845 *len_incl_bad += block_len;
849 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
851 void put_mtd_device(struct mtd_info *mtd)
853 mutex_lock(&mtd_table_mutex);
854 __put_mtd_device(mtd);
855 mutex_unlock(&mtd_table_mutex);
858 EXPORT_SYMBOL_GPL(put_mtd_device);
860 void __put_mtd_device(struct mtd_info *mtd)
863 BUG_ON(mtd->usecount < 0);
865 if (mtd->_put_device)
866 mtd->_put_device(mtd);
868 module_put(mtd->owner);
870 EXPORT_SYMBOL_GPL(__put_mtd_device);
873 * Erase is an asynchronous operation. Device drivers are supposed
874 * to call instr->callback() whenever the operation completes, even
875 * if it completes with a failure.
876 * Callers are supposed to pass a callback function and wait for it
877 * to be called before writing to the block.
879 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
881 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
883 if (!(mtd->flags & MTD_WRITEABLE))
885 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
887 instr->state = MTD_ERASE_DONE;
888 mtd_erase_callback(instr);
891 return mtd->_erase(mtd, instr);
893 EXPORT_SYMBOL_GPL(mtd_erase);
897 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
899 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
900 void **virt, resource_size_t *phys)
908 if (from < 0 || from > mtd->size || len > mtd->size - from)
912 return mtd->_point(mtd, from, len, retlen, virt, phys);
914 EXPORT_SYMBOL_GPL(mtd_point);
916 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
917 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
921 if (from < 0 || from > mtd->size || len > mtd->size - from)
925 return mtd->_unpoint(mtd, from, len);
927 EXPORT_SYMBOL_GPL(mtd_unpoint);
931 * Allow NOMMU mmap() to directly map the device (if not NULL)
932 * - return the address to which the offset maps
933 * - return -ENOSYS to indicate refusal to do the mapping
935 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
936 unsigned long offset, unsigned long flags)
938 if (!mtd->_get_unmapped_area)
940 if (offset > mtd->size || len > mtd->size - offset)
942 return mtd->_get_unmapped_area(mtd, len, offset, flags);
944 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
946 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
951 if (from < 0 || from > mtd->size || len > mtd->size - from)
957 * In the absence of an error, drivers return a non-negative integer
958 * representing the maximum number of bitflips that were corrected on
959 * any one ecc region (if applicable; zero otherwise).
962 ret_code = mtd->_read(mtd, from, len, retlen, buf);
963 } else if (mtd->_read_oob) {
964 struct mtd_oob_ops ops = {
969 ret_code = mtd->_read_oob(mtd, from, &ops);
970 *retlen = ops.retlen;
975 if (unlikely(ret_code < 0))
977 if (mtd->ecc_strength == 0)
978 return 0; /* device lacks ecc */
979 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
981 EXPORT_SYMBOL_GPL(mtd_read);
983 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
987 if (to < 0 || to > mtd->size || len > mtd->size - to)
989 if ((!mtd->_write && !mtd->_write_oob) ||
990 !(mtd->flags & MTD_WRITEABLE))
996 struct mtd_oob_ops ops = {
1002 ret = mtd->_write_oob(mtd, to, &ops);
1003 *retlen = ops.retlen;
1007 return mtd->_write(mtd, to, len, retlen, buf);
1009 EXPORT_SYMBOL_GPL(mtd_write);
1012 * In blackbox flight recorder like scenarios we want to make successful writes
1013 * in interrupt context. panic_write() is only intended to be called when its
1014 * known the kernel is about to panic and we need the write to succeed. Since
1015 * the kernel is not going to be running for much longer, this function can
1016 * break locks and delay to ensure the write succeeds (but not sleep).
1018 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1022 if (!mtd->_panic_write)
1024 if (to < 0 || to > mtd->size || len > mtd->size - to)
1026 if (!(mtd->flags & MTD_WRITEABLE))
1030 return mtd->_panic_write(mtd, to, len, retlen, buf);
1032 EXPORT_SYMBOL_GPL(mtd_panic_write);
1034 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1035 struct mtd_oob_ops *ops)
1038 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1039 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1048 if (offs < 0 || offs + ops->len > mtd->size)
1054 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1057 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1058 mtd_div_by_ws(offs, mtd)) *
1059 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1060 if (ops->ooblen > maxooblen)
1067 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1070 ops->retlen = ops->oobretlen = 0;
1072 ret_code = mtd_check_oob_ops(mtd, from, ops);
1076 /* Check the validity of a potential fallback on mtd->_read */
1077 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1081 ret_code = mtd->_read_oob(mtd, from, ops);
1083 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1087 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1088 * similar to mtd->_read(), returning a non-negative integer
1089 * representing max bitflips. In other cases, mtd->_read_oob() may
1090 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1092 if (unlikely(ret_code < 0))
1094 if (mtd->ecc_strength == 0)
1095 return 0; /* device lacks ecc */
1096 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1098 EXPORT_SYMBOL_GPL(mtd_read_oob);
1100 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1101 struct mtd_oob_ops *ops)
1105 ops->retlen = ops->oobretlen = 0;
1107 if (!(mtd->flags & MTD_WRITEABLE))
1110 ret = mtd_check_oob_ops(mtd, to, ops);
1114 /* Check the validity of a potential fallback on mtd->_write */
1115 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1118 if (mtd->_write_oob)
1119 return mtd->_write_oob(mtd, to, ops);
1121 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1124 EXPORT_SYMBOL_GPL(mtd_write_oob);
1127 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1128 * @mtd: MTD device structure
1129 * @section: ECC section. Depending on the layout you may have all the ECC
1130 * bytes stored in a single contiguous section, or one section
1131 * per ECC chunk (and sometime several sections for a single ECC
1133 * @oobecc: OOB region struct filled with the appropriate ECC position
1136 * This function returns ECC section information in the OOB area. If you want
1137 * to get all the ECC bytes information, then you should call
1138 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1140 * Returns zero on success, a negative error code otherwise.
1142 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1143 struct mtd_oob_region *oobecc)
1145 memset(oobecc, 0, sizeof(*oobecc));
1147 if (!mtd || section < 0)
1150 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1153 return mtd->ooblayout->ecc(mtd, section, oobecc);
1155 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1158 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1160 * @mtd: MTD device structure
1161 * @section: Free section you are interested in. Depending on the layout
1162 * you may have all the free bytes stored in a single contiguous
1163 * section, or one section per ECC chunk plus an extra section
1164 * for the remaining bytes (or other funky layout).
1165 * @oobfree: OOB region struct filled with the appropriate free position
1168 * This function returns free bytes position in the OOB area. If you want
1169 * to get all the free bytes information, then you should call
1170 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1172 * Returns zero on success, a negative error code otherwise.
1174 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1175 struct mtd_oob_region *oobfree)
1177 memset(oobfree, 0, sizeof(*oobfree));
1179 if (!mtd || section < 0)
1182 if (!mtd->ooblayout || !mtd->ooblayout->free)
1185 return mtd->ooblayout->free(mtd, section, oobfree);
1187 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1190 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1191 * @mtd: mtd info structure
1192 * @byte: the byte we are searching for
1193 * @sectionp: pointer where the section id will be stored
1194 * @oobregion: used to retrieve the ECC position
1195 * @iter: iterator function. Should be either mtd_ooblayout_free or
1196 * mtd_ooblayout_ecc depending on the region type you're searching for
1198 * This function returns the section id and oobregion information of a
1199 * specific byte. For example, say you want to know where the 4th ECC byte is
1200 * stored, you'll use:
1202 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1204 * Returns zero on success, a negative error code otherwise.
1206 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1207 int *sectionp, struct mtd_oob_region *oobregion,
1208 int (*iter)(struct mtd_info *,
1210 struct mtd_oob_region *oobregion))
1212 int pos = 0, ret, section = 0;
1214 memset(oobregion, 0, sizeof(*oobregion));
1217 ret = iter(mtd, section, oobregion);
1221 if (pos + oobregion->length > byte)
1224 pos += oobregion->length;
1229 * Adjust region info to make it start at the beginning at the
1232 oobregion->offset += byte - pos;
1233 oobregion->length -= byte - pos;
1234 *sectionp = section;
1240 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1242 * @mtd: mtd info structure
1243 * @eccbyte: the byte we are searching for
1244 * @sectionp: pointer where the section id will be stored
1245 * @oobregion: OOB region information
1247 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1250 * Returns zero on success, a negative error code otherwise.
1252 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1254 struct mtd_oob_region *oobregion)
1256 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1259 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1262 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1263 * @mtd: mtd info structure
1264 * @buf: destination buffer to store OOB bytes
1265 * @oobbuf: OOB buffer
1266 * @start: first byte to retrieve
1267 * @nbytes: number of bytes to retrieve
1268 * @iter: section iterator
1270 * Extract bytes attached to a specific category (ECC or free)
1271 * from the OOB buffer and copy them into buf.
1273 * Returns zero on success, a negative error code otherwise.
1275 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1276 const u8 *oobbuf, int start, int nbytes,
1277 int (*iter)(struct mtd_info *,
1279 struct mtd_oob_region *oobregion))
1281 struct mtd_oob_region oobregion;
1284 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1290 cnt = min_t(int, nbytes, oobregion.length);
1291 memcpy(buf, oobbuf + oobregion.offset, cnt);
1298 ret = iter(mtd, ++section, &oobregion);
1305 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1306 * @mtd: mtd info structure
1307 * @buf: source buffer to get OOB bytes from
1308 * @oobbuf: OOB buffer
1309 * @start: first OOB byte to set
1310 * @nbytes: number of OOB bytes to set
1311 * @iter: section iterator
1313 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1314 * is selected by passing the appropriate iterator.
1316 * Returns zero on success, a negative error code otherwise.
1318 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1319 u8 *oobbuf, int start, int nbytes,
1320 int (*iter)(struct mtd_info *,
1322 struct mtd_oob_region *oobregion))
1324 struct mtd_oob_region oobregion;
1327 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1333 cnt = min_t(int, nbytes, oobregion.length);
1334 memcpy(oobbuf + oobregion.offset, buf, cnt);
1341 ret = iter(mtd, ++section, &oobregion);
1348 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1349 * @mtd: mtd info structure
1350 * @iter: category iterator
1352 * Count the number of bytes in a given category.
1354 * Returns a positive value on success, a negative error code otherwise.
1356 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1357 int (*iter)(struct mtd_info *,
1359 struct mtd_oob_region *oobregion))
1361 struct mtd_oob_region oobregion;
1362 int section = 0, ret, nbytes = 0;
1365 ret = iter(mtd, section++, &oobregion);
1372 nbytes += oobregion.length;
1379 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1380 * @mtd: mtd info structure
1381 * @eccbuf: destination buffer to store ECC bytes
1382 * @oobbuf: OOB buffer
1383 * @start: first ECC byte to retrieve
1384 * @nbytes: number of ECC bytes to retrieve
1386 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1388 * Returns zero on success, a negative error code otherwise.
1390 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1391 const u8 *oobbuf, int start, int nbytes)
1393 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1396 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1399 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1400 * @mtd: mtd info structure
1401 * @eccbuf: source buffer to get ECC bytes from
1402 * @oobbuf: OOB buffer
1403 * @start: first ECC byte to set
1404 * @nbytes: number of ECC bytes to set
1406 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1408 * Returns zero on success, a negative error code otherwise.
1410 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1411 u8 *oobbuf, int start, int nbytes)
1413 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1416 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1419 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1420 * @mtd: mtd info structure
1421 * @databuf: destination buffer to store ECC bytes
1422 * @oobbuf: OOB buffer
1423 * @start: first ECC byte to retrieve
1424 * @nbytes: number of ECC bytes to retrieve
1426 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1428 * Returns zero on success, a negative error code otherwise.
1430 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1431 const u8 *oobbuf, int start, int nbytes)
1433 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1434 mtd_ooblayout_free);
1436 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1439 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1440 * @mtd: mtd info structure
1441 * @eccbuf: source buffer to get data bytes from
1442 * @oobbuf: OOB buffer
1443 * @start: first ECC byte to set
1444 * @nbytes: number of ECC bytes to set
1446 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1448 * Returns zero on success, a negative error code otherwise.
1450 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1451 u8 *oobbuf, int start, int nbytes)
1453 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1454 mtd_ooblayout_free);
1456 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1459 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1460 * @mtd: mtd info structure
1462 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1464 * Returns zero on success, a negative error code otherwise.
1466 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1468 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1470 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1473 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1474 * @mtd: mtd info structure
1476 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1478 * Returns zero on success, a negative error code otherwise.
1480 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1482 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1484 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1487 * Method to access the protection register area, present in some flash
1488 * devices. The user data is one time programmable but the factory data is read
1491 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1492 struct otp_info *buf)
1494 if (!mtd->_get_fact_prot_info)
1498 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1500 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1502 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1503 size_t *retlen, u_char *buf)
1506 if (!mtd->_read_fact_prot_reg)
1510 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1512 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1514 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1515 struct otp_info *buf)
1517 if (!mtd->_get_user_prot_info)
1521 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1523 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1525 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1526 size_t *retlen, u_char *buf)
1529 if (!mtd->_read_user_prot_reg)
1533 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1535 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1537 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1538 size_t *retlen, u_char *buf)
1543 if (!mtd->_write_user_prot_reg)
1547 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1552 * If no data could be written at all, we are out of memory and
1553 * must return -ENOSPC.
1555 return (*retlen) ? 0 : -ENOSPC;
1557 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1559 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1561 if (!mtd->_lock_user_prot_reg)
1565 return mtd->_lock_user_prot_reg(mtd, from, len);
1567 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1569 /* Chip-supported device locking */
1570 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1574 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1578 return mtd->_lock(mtd, ofs, len);
1580 EXPORT_SYMBOL_GPL(mtd_lock);
1582 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1586 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1590 return mtd->_unlock(mtd, ofs, len);
1592 EXPORT_SYMBOL_GPL(mtd_unlock);
1594 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1596 if (!mtd->_is_locked)
1598 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1602 return mtd->_is_locked(mtd, ofs, len);
1604 EXPORT_SYMBOL_GPL(mtd_is_locked);
1606 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1608 if (ofs < 0 || ofs > mtd->size)
1610 if (!mtd->_block_isreserved)
1612 return mtd->_block_isreserved(mtd, ofs);
1614 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1616 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1618 if (ofs < 0 || ofs > mtd->size)
1620 if (!mtd->_block_isbad)
1622 return mtd->_block_isbad(mtd, ofs);
1624 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1626 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1628 if (!mtd->_block_markbad)
1630 if (ofs < 0 || ofs > mtd->size)
1632 if (!(mtd->flags & MTD_WRITEABLE))
1634 return mtd->_block_markbad(mtd, ofs);
1636 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1640 * default_mtd_writev - the default writev method
1641 * @mtd: mtd device description object pointer
1642 * @vecs: the vectors to write
1643 * @count: count of vectors in @vecs
1644 * @to: the MTD device offset to write to
1645 * @retlen: on exit contains the count of bytes written to the MTD device.
1647 * This function returns zero in case of success and a negative error code in
1650 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1651 unsigned long count, loff_t to, size_t *retlen)
1654 size_t totlen = 0, thislen;
1657 for (i = 0; i < count; i++) {
1658 if (!vecs[i].iov_len)
1660 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1663 if (ret || thislen != vecs[i].iov_len)
1665 to += vecs[i].iov_len;
1672 * mtd_writev - the vector-based MTD write method
1673 * @mtd: mtd device description object pointer
1674 * @vecs: the vectors to write
1675 * @count: count of vectors in @vecs
1676 * @to: the MTD device offset to write to
1677 * @retlen: on exit contains the count of bytes written to the MTD device.
1679 * This function returns zero in case of success and a negative error code in
1682 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1683 unsigned long count, loff_t to, size_t *retlen)
1686 if (!(mtd->flags & MTD_WRITEABLE))
1689 return default_mtd_writev(mtd, vecs, count, to, retlen);
1690 return mtd->_writev(mtd, vecs, count, to, retlen);
1692 EXPORT_SYMBOL_GPL(mtd_writev);
1695 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1696 * @mtd: mtd device description object pointer
1697 * @size: a pointer to the ideal or maximum size of the allocation, points
1698 * to the actual allocation size on success.
1700 * This routine attempts to allocate a contiguous kernel buffer up to
1701 * the specified size, backing off the size of the request exponentially
1702 * until the request succeeds or until the allocation size falls below
1703 * the system page size. This attempts to make sure it does not adversely
1704 * impact system performance, so when allocating more than one page, we
1705 * ask the memory allocator to avoid re-trying, swapping, writing back
1706 * or performing I/O.
1708 * Note, this function also makes sure that the allocated buffer is aligned to
1709 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1711 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1712 * to handle smaller (i.e. degraded) buffer allocations under low- or
1713 * fragmented-memory situations where such reduced allocations, from a
1714 * requested ideal, are allowed.
1716 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1718 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1720 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1721 __GFP_NORETRY | __GFP_NO_KSWAPD;
1722 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1725 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1727 while (*size > min_alloc) {
1728 kbuf = kmalloc(*size, flags);
1733 *size = ALIGN(*size, mtd->writesize);
1737 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1738 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1740 return kmalloc(*size, GFP_KERNEL);
1742 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1745 #ifdef CONFIG_PROC_FS
1747 /*====================================================================*/
1748 /* Support for /proc/mtd */
1750 static int mtd_proc_show(struct seq_file *m, void *v)
1752 struct mtd_info *mtd;
1754 seq_puts(m, "dev: size erasesize name\n");
1755 mutex_lock(&mtd_table_mutex);
1756 mtd_for_each_device(mtd) {
1757 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1758 mtd->index, (unsigned long long)mtd->size,
1759 mtd->erasesize, mtd->name);
1761 mutex_unlock(&mtd_table_mutex);
1765 static int mtd_proc_open(struct inode *inode, struct file *file)
1767 return single_open(file, mtd_proc_show, NULL);
1770 static const struct file_operations mtd_proc_ops = {
1771 .open = mtd_proc_open,
1773 .llseek = seq_lseek,
1774 .release = single_release,
1776 #endif /* CONFIG_PROC_FS */
1778 /*====================================================================*/
1782 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1786 ret = bdi_init(bdi);
1788 ret = bdi_register(bdi, NULL, "%s", name);
1796 static struct proc_dir_entry *proc_mtd;
1798 static int __init init_mtd(void)
1802 ret = class_register(&mtd_class);
1806 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1810 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1814 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1818 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1820 ret = init_mtdchar();
1828 remove_proc_entry("mtd", NULL);
1830 bdi_destroy(&mtd_bdi_ro_mappable);
1832 bdi_destroy(&mtd_bdi_unmappable);
1834 class_unregister(&mtd_class);
1836 pr_err("Error registering mtd class or bdi: %d\n", ret);
1840 static void __exit cleanup_mtd(void)
1844 remove_proc_entry("mtd", NULL);
1845 class_unregister(&mtd_class);
1846 bdi_destroy(&mtd_bdi_unmappable);
1847 bdi_destroy(&mtd_bdi_ro_mappable);
1848 bdi_destroy(&mtd_bdi_rw_mappable);
1851 module_init(init_mtd);
1852 module_exit(cleanup_mtd);
1855 MODULE_LICENSE("GPL");
1856 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1857 MODULE_DESCRIPTION("Core MTD registration and access routines");