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/bug.h>
32 #include <linux/err.h>
33 #include <ubi_uboot.h>
36 #include <linux/log2.h>
37 #include <linux/mtd/mtd.h>
38 #include <linux/mtd/partitions.h>
44 * backing device capabilities for non-mappable devices (such as NAND flash)
45 * - permits private mappings, copies are taken of the data
47 static struct backing_dev_info mtd_bdi_unmappable = {
48 .capabilities = BDI_CAP_MAP_COPY,
52 * backing device capabilities for R/O mappable devices (such as ROM)
53 * - permits private mappings, copies are taken of the data
54 * - permits non-writable shared mappings
56 static struct backing_dev_info mtd_bdi_ro_mappable = {
57 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
58 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
62 * backing device capabilities for writable mappable devices (such as RAM)
63 * - permits private mappings, copies are taken of the data
64 * - permits non-writable shared mappings
66 static struct backing_dev_info mtd_bdi_rw_mappable = {
67 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
68 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
72 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
73 static int mtd_cls_resume(struct device *dev);
75 static struct class mtd_class = {
78 .suspend = mtd_cls_suspend,
79 .resume = mtd_cls_resume,
90 struct idr_layer id[MAX_IDR_ID];
94 #define DEFINE_IDR(name) struct idr name;
96 void idr_remove(struct idr *idp, int id)
98 if (idp->id[id].used) {
105 void *idr_find(struct idr *idp, int id)
107 if (idp->id[id].used)
108 return idp->id[id].ptr;
113 void *idr_get_next(struct idr *idp, int *next)
118 ret = idr_find(idp, id);
121 if (!idp->id[id].used)
131 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
133 struct idr_layer *idl;
136 while (i < MAX_IDR_ID) {
138 if (idl->used == 0) {
150 static DEFINE_IDR(mtd_idr);
152 /* These are exported solely for the purpose of mtd_blkdevs.c. You
153 should not use them for _anything_ else */
154 DEFINE_MUTEX(mtd_table_mutex);
155 EXPORT_SYMBOL_GPL(mtd_table_mutex);
157 struct mtd_info *__mtd_next_device(int i)
159 return idr_get_next(&mtd_idr, &i);
161 EXPORT_SYMBOL_GPL(__mtd_next_device);
163 bool mtd_dev_list_updated(void)
165 if (mtd_idr.updated) {
166 mtd_idr.updated = false;
174 static LIST_HEAD(mtd_notifiers);
177 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
179 /* REVISIT once MTD uses the driver model better, whoever allocates
180 * the mtd_info will probably want to use the release() hook...
182 static void mtd_release(struct device *dev)
184 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
185 dev_t index = MTD_DEVT(mtd->index);
187 /* remove /dev/mtdXro node if needed */
189 device_destroy(&mtd_class, index + 1);
192 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
194 struct mtd_info *mtd = dev_get_drvdata(dev);
196 return mtd ? mtd_suspend(mtd) : 0;
199 static int mtd_cls_resume(struct device *dev)
201 struct mtd_info *mtd = dev_get_drvdata(dev);
208 static ssize_t mtd_type_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct mtd_info *mtd = dev_get_drvdata(dev);
236 case MTD_MLCNANDFLASH:
243 return snprintf(buf, PAGE_SIZE, "%s\n", type);
245 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
247 static ssize_t mtd_flags_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct mtd_info *mtd = dev_get_drvdata(dev);
252 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
255 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
257 static ssize_t mtd_size_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 struct mtd_info *mtd = dev_get_drvdata(dev);
262 return snprintf(buf, PAGE_SIZE, "%llu\n",
263 (unsigned long long)mtd->size);
266 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
268 static ssize_t mtd_erasesize_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
271 struct mtd_info *mtd = dev_get_drvdata(dev);
273 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
276 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
278 static ssize_t mtd_writesize_show(struct device *dev,
279 struct device_attribute *attr, char *buf)
281 struct mtd_info *mtd = dev_get_drvdata(dev);
283 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
286 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
288 static ssize_t mtd_subpagesize_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct mtd_info *mtd = dev_get_drvdata(dev);
292 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
294 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
297 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
299 static ssize_t mtd_oobsize_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct mtd_info *mtd = dev_get_drvdata(dev);
304 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
307 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
309 static ssize_t mtd_numeraseregions_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct mtd_info *mtd = dev_get_drvdata(dev);
314 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
317 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
320 static ssize_t mtd_name_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
323 struct mtd_info *mtd = dev_get_drvdata(dev);
325 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
328 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
330 static ssize_t mtd_ecc_strength_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct mtd_info *mtd = dev_get_drvdata(dev);
335 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
337 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
339 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
340 struct device_attribute *attr,
343 struct mtd_info *mtd = dev_get_drvdata(dev);
345 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
348 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
352 struct mtd_info *mtd = dev_get_drvdata(dev);
353 unsigned int bitflip_threshold;
356 retval = kstrtouint(buf, 0, &bitflip_threshold);
360 mtd->bitflip_threshold = bitflip_threshold;
363 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
364 mtd_bitflip_threshold_show,
365 mtd_bitflip_threshold_store);
367 static ssize_t mtd_ecc_step_size_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct mtd_info *mtd = dev_get_drvdata(dev);
372 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
375 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
377 static struct attribute *mtd_attrs[] = {
379 &dev_attr_flags.attr,
381 &dev_attr_erasesize.attr,
382 &dev_attr_writesize.attr,
383 &dev_attr_subpagesize.attr,
384 &dev_attr_oobsize.attr,
385 &dev_attr_numeraseregions.attr,
387 &dev_attr_ecc_strength.attr,
388 &dev_attr_ecc_step_size.attr,
389 &dev_attr_bitflip_threshold.attr,
392 ATTRIBUTE_GROUPS(mtd);
394 static struct device_type mtd_devtype = {
396 .groups = mtd_groups,
397 .release = mtd_release,
402 * add_mtd_device - register an MTD device
403 * @mtd: pointer to new MTD device info structure
405 * Add a device to the list of MTD devices present in the system, and
406 * notify each currently active MTD 'user' of its arrival. Returns
407 * zero on success or 1 on failure, which currently will only happen
408 * if there is insufficient memory or a sysfs error.
411 int add_mtd_device(struct mtd_info *mtd)
414 struct mtd_notifier *not;
419 if (!mtd->backing_dev_info) {
422 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
425 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
428 mtd->backing_dev_info = &mtd_bdi_unmappable;
434 BUG_ON(mtd->writesize == 0);
435 mutex_lock(&mtd_table_mutex);
437 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
444 INIT_LIST_HEAD(&mtd->partitions);
446 /* default value if not set by driver */
447 if (mtd->bitflip_threshold == 0)
448 mtd->bitflip_threshold = mtd->ecc_strength;
450 if (is_power_of_2(mtd->erasesize))
451 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
453 mtd->erasesize_shift = 0;
455 if (is_power_of_2(mtd->writesize))
456 mtd->writesize_shift = ffs(mtd->writesize) - 1;
458 mtd->writesize_shift = 0;
460 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
461 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
463 /* Some chips always power up locked. Unlock them now */
464 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
465 error = mtd_unlock(mtd, 0, mtd->size);
466 if (error && error != -EOPNOTSUPP)
468 "%s: unlock failed, writes may not work\n",
473 /* Caller should have set dev.parent to match the
476 mtd->dev.type = &mtd_devtype;
477 mtd->dev.class = &mtd_class;
478 mtd->dev.devt = MTD_DEVT(i);
479 dev_set_name(&mtd->dev, "mtd%d", i);
480 dev_set_drvdata(&mtd->dev, mtd);
481 if (device_register(&mtd->dev) != 0)
485 device_create(&mtd_class, mtd->dev.parent,
489 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
490 /* No need to get a refcount on the module containing
491 the notifier, since we hold the mtd_table_mutex */
492 list_for_each_entry(not, &mtd_notifiers, list)
495 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
498 mutex_unlock(&mtd_table_mutex);
499 /* We _know_ we aren't being removed, because
500 our caller is still holding us here. So none
501 of this try_ nonsense, and no bitching about it
503 __module_get(THIS_MODULE);
508 idr_remove(&mtd_idr, i);
511 mutex_unlock(&mtd_table_mutex);
516 * del_mtd_device - unregister an MTD device
517 * @mtd: pointer to MTD device info structure
519 * Remove a device from the list of MTD devices present in the system,
520 * and notify each currently active MTD 'user' of its departure.
521 * Returns zero on success or 1 on failure, which currently will happen
522 * if the requested device does not appear to be present in the list.
525 int del_mtd_device(struct mtd_info *mtd)
529 struct mtd_notifier *not;
532 ret = del_mtd_partitions(mtd);
534 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
539 mutex_lock(&mtd_table_mutex);
541 if (idr_find(&mtd_idr, mtd->index) != mtd) {
547 /* No need to get a refcount on the module containing
548 the notifier, since we hold the mtd_table_mutex */
549 list_for_each_entry(not, &mtd_notifiers, list)
554 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
555 mtd->index, mtd->name, mtd->usecount);
559 device_unregister(&mtd->dev);
562 idr_remove(&mtd_idr, mtd->index);
564 module_put(THIS_MODULE);
569 mutex_unlock(&mtd_table_mutex);
575 * mtd_device_parse_register - parse partitions and register an MTD device.
577 * @mtd: the MTD device to register
578 * @types: the list of MTD partition probes to try, see
579 * 'parse_mtd_partitions()' for more information
580 * @parser_data: MTD partition parser-specific data
581 * @parts: fallback partition information to register, if parsing fails;
582 * only valid if %nr_parts > %0
583 * @nr_parts: the number of partitions in parts, if zero then the full
584 * MTD device is registered if no partition info is found
586 * This function aggregates MTD partitions parsing (done by
587 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
588 * basically follows the most common pattern found in many MTD drivers:
590 * * It first tries to probe partitions on MTD device @mtd using parsers
591 * specified in @types (if @types is %NULL, then the default list of parsers
592 * is used, see 'parse_mtd_partitions()' for more information). If none are
593 * found this functions tries to fallback to information specified in
595 * * If any partitioning info was found, this function registers the found
597 * * If no partitions were found this function just registers the MTD device
600 * Returns zero in case of success and a negative error code in case of failure.
602 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
603 struct mtd_part_parser_data *parser_data,
604 const struct mtd_partition *parts,
608 struct mtd_partition *real_parts;
610 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
611 if (err <= 0 && nr_parts && parts) {
612 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
621 err = add_mtd_partitions(mtd, real_parts, err);
623 } else if (err == 0) {
624 err = add_mtd_device(mtd);
631 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
634 * mtd_device_unregister - unregister an existing MTD device.
636 * @master: the MTD device to unregister. This will unregister both the master
637 * and any partitions if registered.
639 int mtd_device_unregister(struct mtd_info *master)
643 err = del_mtd_partitions(master);
647 if (!device_is_registered(&master->dev))
650 return del_mtd_device(master);
652 EXPORT_SYMBOL_GPL(mtd_device_unregister);
655 * register_mtd_user - register a 'user' of MTD devices.
656 * @new: pointer to notifier info structure
658 * Registers a pair of callbacks function to be called upon addition
659 * or removal of MTD devices. Causes the 'add' callback to be immediately
660 * invoked for each MTD device currently present in the system.
662 void register_mtd_user (struct mtd_notifier *new)
664 struct mtd_info *mtd;
666 mutex_lock(&mtd_table_mutex);
668 list_add(&new->list, &mtd_notifiers);
670 __module_get(THIS_MODULE);
672 mtd_for_each_device(mtd)
675 mutex_unlock(&mtd_table_mutex);
677 EXPORT_SYMBOL_GPL(register_mtd_user);
680 * unregister_mtd_user - unregister a 'user' of MTD devices.
681 * @old: pointer to notifier info structure
683 * Removes a callback function pair from the list of 'users' to be
684 * notified upon addition or removal of MTD devices. Causes the
685 * 'remove' callback to be immediately invoked for each MTD device
686 * currently present in the system.
688 int unregister_mtd_user (struct mtd_notifier *old)
690 struct mtd_info *mtd;
692 mutex_lock(&mtd_table_mutex);
694 module_put(THIS_MODULE);
696 mtd_for_each_device(mtd)
699 list_del(&old->list);
700 mutex_unlock(&mtd_table_mutex);
703 EXPORT_SYMBOL_GPL(unregister_mtd_user);
707 * get_mtd_device - obtain a validated handle for an MTD device
708 * @mtd: last known address of the required MTD device
709 * @num: internal device number of the required MTD device
711 * Given a number and NULL address, return the num'th entry in the device
712 * table, if any. Given an address and num == -1, search the device table
713 * for a device with that address and return if it's still present. Given
714 * both, return the num'th driver only if its address matches. Return
717 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
719 struct mtd_info *ret = NULL, *other;
722 mutex_lock(&mtd_table_mutex);
725 mtd_for_each_device(other) {
731 } else if (num >= 0) {
732 ret = idr_find(&mtd_idr, num);
733 if (mtd && mtd != ret)
742 err = __get_mtd_device(ret);
746 mutex_unlock(&mtd_table_mutex);
749 EXPORT_SYMBOL_GPL(get_mtd_device);
752 int __get_mtd_device(struct mtd_info *mtd)
756 if (!try_module_get(mtd->owner))
759 if (mtd->_get_device) {
760 err = mtd->_get_device(mtd);
763 module_put(mtd->owner);
770 EXPORT_SYMBOL_GPL(__get_mtd_device);
773 * get_mtd_device_nm - obtain a validated handle for an MTD device by
775 * @name: MTD device name to open
777 * This function returns MTD device description structure in case of
778 * success and an error code in case of failure.
780 struct mtd_info *get_mtd_device_nm(const char *name)
783 struct mtd_info *mtd = NULL, *other;
785 mutex_lock(&mtd_table_mutex);
787 mtd_for_each_device(other) {
788 if (!strcmp(name, other->name)) {
797 err = __get_mtd_device(mtd);
801 mutex_unlock(&mtd_table_mutex);
805 mutex_unlock(&mtd_table_mutex);
808 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
810 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
812 * mtd_get_len_incl_bad
814 * Check if length including bad blocks fits into device.
816 * @param mtd an MTD device
817 * @param offset offset in flash
818 * @param length image length
819 * @return image length including bad blocks in *len_incl_bad and whether or not
820 * the length returned was truncated in *truncated
822 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
823 const uint64_t length, uint64_t *len_incl_bad,
829 if (!mtd->_block_isbad) {
830 *len_incl_bad = length;
834 uint64_t len_excl_bad = 0;
837 while (len_excl_bad < length) {
838 if (offset >= mtd->size) {
843 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
845 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
846 len_excl_bad += block_len;
848 *len_incl_bad += block_len;
852 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
854 void put_mtd_device(struct mtd_info *mtd)
856 mutex_lock(&mtd_table_mutex);
857 __put_mtd_device(mtd);
858 mutex_unlock(&mtd_table_mutex);
861 EXPORT_SYMBOL_GPL(put_mtd_device);
863 void __put_mtd_device(struct mtd_info *mtd)
866 BUG_ON(mtd->usecount < 0);
868 if (mtd->_put_device)
869 mtd->_put_device(mtd);
871 module_put(mtd->owner);
873 EXPORT_SYMBOL_GPL(__put_mtd_device);
876 * Erase is an asynchronous operation. Device drivers are supposed
877 * to call instr->callback() whenever the operation completes, even
878 * if it completes with a failure.
879 * Callers are supposed to pass a callback function and wait for it
880 * to be called before writing to the block.
882 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
884 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
886 if (!(mtd->flags & MTD_WRITEABLE))
888 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
890 instr->state = MTD_ERASE_DONE;
891 mtd_erase_callback(instr);
894 return mtd->_erase(mtd, instr);
896 EXPORT_SYMBOL_GPL(mtd_erase);
900 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
902 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
903 void **virt, resource_size_t *phys)
911 if (from < 0 || from > mtd->size || len > mtd->size - from)
915 return mtd->_point(mtd, from, len, retlen, virt, phys);
917 EXPORT_SYMBOL_GPL(mtd_point);
919 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
920 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
924 if (from < 0 || from > mtd->size || len > mtd->size - from)
928 return mtd->_unpoint(mtd, from, len);
930 EXPORT_SYMBOL_GPL(mtd_unpoint);
934 * Allow NOMMU mmap() to directly map the device (if not NULL)
935 * - return the address to which the offset maps
936 * - return -ENOSYS to indicate refusal to do the mapping
938 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
939 unsigned long offset, unsigned long flags)
941 if (!mtd->_get_unmapped_area)
943 if (offset > mtd->size || len > mtd->size - offset)
945 return mtd->_get_unmapped_area(mtd, len, offset, flags);
947 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
949 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
954 if (from < 0 || from > mtd->size || len > mtd->size - from)
960 * In the absence of an error, drivers return a non-negative integer
961 * representing the maximum number of bitflips that were corrected on
962 * any one ecc region (if applicable; zero otherwise).
965 ret_code = mtd->_read(mtd, from, len, retlen, buf);
966 } else if (mtd->_read_oob) {
967 struct mtd_oob_ops ops = {
972 ret_code = mtd->_read_oob(mtd, from, &ops);
973 *retlen = ops.retlen;
978 if (unlikely(ret_code < 0))
980 if (mtd->ecc_strength == 0)
981 return 0; /* device lacks ecc */
982 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
984 EXPORT_SYMBOL_GPL(mtd_read);
986 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
990 if (to < 0 || to > mtd->size || len > mtd->size - to)
992 if ((!mtd->_write && !mtd->_write_oob) ||
993 !(mtd->flags & MTD_WRITEABLE))
999 struct mtd_oob_ops ops = {
1001 .datbuf = (u8 *)buf,
1005 ret = mtd->_write_oob(mtd, to, &ops);
1006 *retlen = ops.retlen;
1010 return mtd->_write(mtd, to, len, retlen, buf);
1012 EXPORT_SYMBOL_GPL(mtd_write);
1015 * In blackbox flight recorder like scenarios we want to make successful writes
1016 * in interrupt context. panic_write() is only intended to be called when its
1017 * known the kernel is about to panic and we need the write to succeed. Since
1018 * the kernel is not going to be running for much longer, this function can
1019 * break locks and delay to ensure the write succeeds (but not sleep).
1021 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1025 if (!mtd->_panic_write)
1027 if (to < 0 || to > mtd->size || len > mtd->size - to)
1029 if (!(mtd->flags & MTD_WRITEABLE))
1033 return mtd->_panic_write(mtd, to, len, retlen, buf);
1035 EXPORT_SYMBOL_GPL(mtd_panic_write);
1037 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1038 struct mtd_oob_ops *ops)
1041 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1042 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1051 if (offs < 0 || offs + ops->len > mtd->size)
1057 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1060 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1061 mtd_div_by_ws(offs, mtd)) *
1062 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1063 if (ops->ooblen > maxooblen)
1070 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1073 ops->retlen = ops->oobretlen = 0;
1075 ret_code = mtd_check_oob_ops(mtd, from, ops);
1079 /* Check the validity of a potential fallback on mtd->_read */
1080 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1084 ret_code = mtd->_read_oob(mtd, from, ops);
1086 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1090 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1091 * similar to mtd->_read(), returning a non-negative integer
1092 * representing max bitflips. In other cases, mtd->_read_oob() may
1093 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1095 if (unlikely(ret_code < 0))
1097 if (mtd->ecc_strength == 0)
1098 return 0; /* device lacks ecc */
1099 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1101 EXPORT_SYMBOL_GPL(mtd_read_oob);
1103 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1104 struct mtd_oob_ops *ops)
1108 ops->retlen = ops->oobretlen = 0;
1110 if (!(mtd->flags & MTD_WRITEABLE))
1113 ret = mtd_check_oob_ops(mtd, to, ops);
1117 /* Check the validity of a potential fallback on mtd->_write */
1118 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1121 if (mtd->_write_oob)
1122 return mtd->_write_oob(mtd, to, ops);
1124 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1127 EXPORT_SYMBOL_GPL(mtd_write_oob);
1130 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1131 * @mtd: MTD device structure
1132 * @section: ECC section. Depending on the layout you may have all the ECC
1133 * bytes stored in a single contiguous section, or one section
1134 * per ECC chunk (and sometime several sections for a single ECC
1136 * @oobecc: OOB region struct filled with the appropriate ECC position
1139 * This function returns ECC section information in the OOB area. If you want
1140 * to get all the ECC bytes information, then you should call
1141 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1143 * Returns zero on success, a negative error code otherwise.
1145 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1146 struct mtd_oob_region *oobecc)
1148 memset(oobecc, 0, sizeof(*oobecc));
1150 if (!mtd || section < 0)
1153 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1156 return mtd->ooblayout->ecc(mtd, section, oobecc);
1158 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1161 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1163 * @mtd: MTD device structure
1164 * @section: Free section you are interested in. Depending on the layout
1165 * you may have all the free bytes stored in a single contiguous
1166 * section, or one section per ECC chunk plus an extra section
1167 * for the remaining bytes (or other funky layout).
1168 * @oobfree: OOB region struct filled with the appropriate free position
1171 * This function returns free bytes position in the OOB area. If you want
1172 * to get all the free bytes information, then you should call
1173 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1175 * Returns zero on success, a negative error code otherwise.
1177 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1178 struct mtd_oob_region *oobfree)
1180 memset(oobfree, 0, sizeof(*oobfree));
1182 if (!mtd || section < 0)
1185 if (!mtd->ooblayout || !mtd->ooblayout->rfree)
1188 return mtd->ooblayout->rfree(mtd, section, oobfree);
1190 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1193 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1194 * @mtd: mtd info structure
1195 * @byte: the byte we are searching for
1196 * @sectionp: pointer where the section id will be stored
1197 * @oobregion: used to retrieve the ECC position
1198 * @iter: iterator function. Should be either mtd_ooblayout_free or
1199 * mtd_ooblayout_ecc depending on the region type you're searching for
1201 * This function returns the section id and oobregion information of a
1202 * specific byte. For example, say you want to know where the 4th ECC byte is
1203 * stored, you'll use:
1205 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1207 * Returns zero on success, a negative error code otherwise.
1209 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1210 int *sectionp, struct mtd_oob_region *oobregion,
1211 int (*iter)(struct mtd_info *,
1213 struct mtd_oob_region *oobregion))
1215 int pos = 0, ret, section = 0;
1217 memset(oobregion, 0, sizeof(*oobregion));
1220 ret = iter(mtd, section, oobregion);
1224 if (pos + oobregion->length > byte)
1227 pos += oobregion->length;
1232 * Adjust region info to make it start at the beginning at the
1235 oobregion->offset += byte - pos;
1236 oobregion->length -= byte - pos;
1237 *sectionp = section;
1243 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1245 * @mtd: mtd info structure
1246 * @eccbyte: the byte we are searching for
1247 * @sectionp: pointer where the section id will be stored
1248 * @oobregion: OOB region information
1250 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1253 * Returns zero on success, a negative error code otherwise.
1255 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1257 struct mtd_oob_region *oobregion)
1259 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1262 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1265 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1266 * @mtd: mtd info structure
1267 * @buf: destination buffer to store OOB bytes
1268 * @oobbuf: OOB buffer
1269 * @start: first byte to retrieve
1270 * @nbytes: number of bytes to retrieve
1271 * @iter: section iterator
1273 * Extract bytes attached to a specific category (ECC or free)
1274 * from the OOB buffer and copy them into buf.
1276 * Returns zero on success, a negative error code otherwise.
1278 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1279 const u8 *oobbuf, int start, int nbytes,
1280 int (*iter)(struct mtd_info *,
1282 struct mtd_oob_region *oobregion))
1284 struct mtd_oob_region oobregion;
1287 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1293 cnt = min_t(int, nbytes, oobregion.length);
1294 memcpy(buf, oobbuf + oobregion.offset, cnt);
1301 ret = iter(mtd, ++section, &oobregion);
1308 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1309 * @mtd: mtd info structure
1310 * @buf: source buffer to get OOB bytes from
1311 * @oobbuf: OOB buffer
1312 * @start: first OOB byte to set
1313 * @nbytes: number of OOB bytes to set
1314 * @iter: section iterator
1316 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1317 * is selected by passing the appropriate iterator.
1319 * Returns zero on success, a negative error code otherwise.
1321 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1322 u8 *oobbuf, int start, int nbytes,
1323 int (*iter)(struct mtd_info *,
1325 struct mtd_oob_region *oobregion))
1327 struct mtd_oob_region oobregion;
1330 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1336 cnt = min_t(int, nbytes, oobregion.length);
1337 memcpy(oobbuf + oobregion.offset, buf, cnt);
1344 ret = iter(mtd, ++section, &oobregion);
1351 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1352 * @mtd: mtd info structure
1353 * @iter: category iterator
1355 * Count the number of bytes in a given category.
1357 * Returns a positive value on success, a negative error code otherwise.
1359 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1360 int (*iter)(struct mtd_info *,
1362 struct mtd_oob_region *oobregion))
1364 struct mtd_oob_region oobregion;
1365 int section = 0, ret, nbytes = 0;
1368 ret = iter(mtd, section++, &oobregion);
1375 nbytes += oobregion.length;
1382 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1383 * @mtd: mtd info structure
1384 * @eccbuf: destination buffer to store ECC bytes
1385 * @oobbuf: OOB buffer
1386 * @start: first ECC byte to retrieve
1387 * @nbytes: number of ECC bytes to retrieve
1389 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1391 * Returns zero on success, a negative error code otherwise.
1393 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1394 const u8 *oobbuf, int start, int nbytes)
1396 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1399 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1402 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1403 * @mtd: mtd info structure
1404 * @eccbuf: source buffer to get ECC bytes from
1405 * @oobbuf: OOB buffer
1406 * @start: first ECC byte to set
1407 * @nbytes: number of ECC bytes to set
1409 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1411 * Returns zero on success, a negative error code otherwise.
1413 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1414 u8 *oobbuf, int start, int nbytes)
1416 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1419 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1422 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1423 * @mtd: mtd info structure
1424 * @databuf: destination buffer to store ECC bytes
1425 * @oobbuf: OOB buffer
1426 * @start: first ECC byte to retrieve
1427 * @nbytes: number of ECC bytes to retrieve
1429 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1431 * Returns zero on success, a negative error code otherwise.
1433 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1434 const u8 *oobbuf, int start, int nbytes)
1436 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1437 mtd_ooblayout_free);
1439 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1442 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1443 * @mtd: mtd info structure
1444 * @eccbuf: source buffer to get data bytes from
1445 * @oobbuf: OOB buffer
1446 * @start: first ECC byte to set
1447 * @nbytes: number of ECC bytes to set
1449 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1451 * Returns zero on success, a negative error code otherwise.
1453 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1454 u8 *oobbuf, int start, int nbytes)
1456 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1457 mtd_ooblayout_free);
1459 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1462 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1463 * @mtd: mtd info structure
1465 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1467 * Returns zero on success, a negative error code otherwise.
1469 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1471 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1473 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1476 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1477 * @mtd: mtd info structure
1479 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1481 * Returns zero on success, a negative error code otherwise.
1483 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1485 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1487 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1490 * Method to access the protection register area, present in some flash
1491 * devices. The user data is one time programmable but the factory data is read
1494 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1495 struct otp_info *buf)
1497 if (!mtd->_get_fact_prot_info)
1501 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1503 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1505 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1506 size_t *retlen, u_char *buf)
1509 if (!mtd->_read_fact_prot_reg)
1513 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1515 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1517 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1518 struct otp_info *buf)
1520 if (!mtd->_get_user_prot_info)
1524 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1526 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1528 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1529 size_t *retlen, u_char *buf)
1532 if (!mtd->_read_user_prot_reg)
1536 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1538 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1540 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1541 size_t *retlen, u_char *buf)
1546 if (!mtd->_write_user_prot_reg)
1550 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1555 * If no data could be written at all, we are out of memory and
1556 * must return -ENOSPC.
1558 return (*retlen) ? 0 : -ENOSPC;
1560 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1562 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1564 if (!mtd->_lock_user_prot_reg)
1568 return mtd->_lock_user_prot_reg(mtd, from, len);
1570 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1572 /* Chip-supported device locking */
1573 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1577 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1581 return mtd->_lock(mtd, ofs, len);
1583 EXPORT_SYMBOL_GPL(mtd_lock);
1585 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1589 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1593 return mtd->_unlock(mtd, ofs, len);
1595 EXPORT_SYMBOL_GPL(mtd_unlock);
1597 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1599 if (!mtd->_is_locked)
1601 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1605 return mtd->_is_locked(mtd, ofs, len);
1607 EXPORT_SYMBOL_GPL(mtd_is_locked);
1609 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1611 if (ofs < 0 || ofs > mtd->size)
1613 if (!mtd->_block_isreserved)
1615 return mtd->_block_isreserved(mtd, ofs);
1617 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1619 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1621 if (ofs < 0 || ofs > mtd->size)
1623 if (!mtd->_block_isbad)
1625 return mtd->_block_isbad(mtd, ofs);
1627 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1629 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1631 if (!mtd->_block_markbad)
1633 if (ofs < 0 || ofs > mtd->size)
1635 if (!(mtd->flags & MTD_WRITEABLE))
1637 return mtd->_block_markbad(mtd, ofs);
1639 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1643 * default_mtd_writev - the default writev method
1644 * @mtd: mtd device description object pointer
1645 * @vecs: the vectors to write
1646 * @count: count of vectors in @vecs
1647 * @to: the MTD device offset to write to
1648 * @retlen: on exit contains the count of bytes written to the MTD device.
1650 * This function returns zero in case of success and a negative error code in
1653 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1654 unsigned long count, loff_t to, size_t *retlen)
1657 size_t totlen = 0, thislen;
1660 for (i = 0; i < count; i++) {
1661 if (!vecs[i].iov_len)
1663 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1666 if (ret || thislen != vecs[i].iov_len)
1668 to += vecs[i].iov_len;
1675 * mtd_writev - the vector-based MTD write method
1676 * @mtd: mtd device description object pointer
1677 * @vecs: the vectors to write
1678 * @count: count of vectors in @vecs
1679 * @to: the MTD device offset to write to
1680 * @retlen: on exit contains the count of bytes written to the MTD device.
1682 * This function returns zero in case of success and a negative error code in
1685 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1686 unsigned long count, loff_t to, size_t *retlen)
1689 if (!(mtd->flags & MTD_WRITEABLE))
1692 return default_mtd_writev(mtd, vecs, count, to, retlen);
1693 return mtd->_writev(mtd, vecs, count, to, retlen);
1695 EXPORT_SYMBOL_GPL(mtd_writev);
1698 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1699 * @mtd: mtd device description object pointer
1700 * @size: a pointer to the ideal or maximum size of the allocation, points
1701 * to the actual allocation size on success.
1703 * This routine attempts to allocate a contiguous kernel buffer up to
1704 * the specified size, backing off the size of the request exponentially
1705 * until the request succeeds or until the allocation size falls below
1706 * the system page size. This attempts to make sure it does not adversely
1707 * impact system performance, so when allocating more than one page, we
1708 * ask the memory allocator to avoid re-trying, swapping, writing back
1709 * or performing I/O.
1711 * Note, this function also makes sure that the allocated buffer is aligned to
1712 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1714 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1715 * to handle smaller (i.e. degraded) buffer allocations under low- or
1716 * fragmented-memory situations where such reduced allocations, from a
1717 * requested ideal, are allowed.
1719 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1721 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1723 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1724 __GFP_NORETRY | __GFP_NO_KSWAPD;
1725 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1728 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1730 while (*size > min_alloc) {
1731 kbuf = kmalloc(*size, flags);
1736 *size = ALIGN(*size, mtd->writesize);
1740 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1741 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1743 return kmalloc(*size, GFP_KERNEL);
1745 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1748 #ifdef CONFIG_PROC_FS
1750 /*====================================================================*/
1751 /* Support for /proc/mtd */
1753 static int mtd_proc_show(struct seq_file *m, void *v)
1755 struct mtd_info *mtd;
1757 seq_puts(m, "dev: size erasesize name\n");
1758 mutex_lock(&mtd_table_mutex);
1759 mtd_for_each_device(mtd) {
1760 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1761 mtd->index, (unsigned long long)mtd->size,
1762 mtd->erasesize, mtd->name);
1764 mutex_unlock(&mtd_table_mutex);
1768 static int mtd_proc_open(struct inode *inode, struct file *file)
1770 return single_open(file, mtd_proc_show, NULL);
1773 static const struct file_operations mtd_proc_ops = {
1774 .open = mtd_proc_open,
1776 .llseek = seq_lseek,
1777 .release = single_release,
1779 #endif /* CONFIG_PROC_FS */
1781 /*====================================================================*/
1785 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1789 ret = bdi_init(bdi);
1791 ret = bdi_register(bdi, NULL, "%s", name);
1799 static struct proc_dir_entry *proc_mtd;
1801 static int __init init_mtd(void)
1805 ret = class_register(&mtd_class);
1809 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1813 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1817 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1821 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1823 ret = init_mtdchar();
1831 remove_proc_entry("mtd", NULL);
1833 bdi_destroy(&mtd_bdi_ro_mappable);
1835 bdi_destroy(&mtd_bdi_unmappable);
1837 class_unregister(&mtd_class);
1839 pr_err("Error registering mtd class or bdi: %d\n", ret);
1843 static void __exit cleanup_mtd(void)
1847 remove_proc_entry("mtd", NULL);
1848 class_unregister(&mtd_class);
1849 bdi_destroy(&mtd_bdi_unmappable);
1850 bdi_destroy(&mtd_bdi_ro_mappable);
1851 bdi_destroy(&mtd_bdi_rw_mappable);
1854 module_init(init_mtd);
1855 module_exit(cleanup_mtd);
1858 MODULE_LICENSE("GPL");
1859 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1860 MODULE_DESCRIPTION("Core MTD registration and access routines");