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,
79 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
89 struct idr_layer id[MAX_IDR_ID];
93 #define DEFINE_IDR(name) struct idr name;
95 void idr_remove(struct idr *idp, int id)
97 if (idp->id[id].used) {
104 void *idr_find(struct idr *idp, int id)
106 if (idp->id[id].used)
107 return idp->id[id].ptr;
112 void *idr_get_next(struct idr *idp, int *next)
117 ret = idr_find(idp, id);
120 if (!idp->id[id].used)
130 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
132 struct idr_layer *idl;
135 while (i < MAX_IDR_ID) {
137 if (idl->used == 0) {
149 static DEFINE_IDR(mtd_idr);
151 /* These are exported solely for the purpose of mtd_blkdevs.c. You
152 should not use them for _anything_ else */
153 DEFINE_MUTEX(mtd_table_mutex);
154 EXPORT_SYMBOL_GPL(mtd_table_mutex);
156 struct mtd_info *__mtd_next_device(int i)
158 return idr_get_next(&mtd_idr, &i);
160 EXPORT_SYMBOL_GPL(__mtd_next_device);
162 bool mtd_dev_list_updated(void)
164 if (mtd_idr.updated) {
165 mtd_idr.updated = false;
173 static LIST_HEAD(mtd_notifiers);
176 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
178 /* REVISIT once MTD uses the driver model better, whoever allocates
179 * the mtd_info will probably want to use the release() hook...
181 static void mtd_release(struct device *dev)
183 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
184 dev_t index = MTD_DEVT(mtd->index);
186 /* remove /dev/mtdXro node if needed */
188 device_destroy(&mtd_class, index + 1);
191 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
193 struct mtd_info *mtd = dev_get_drvdata(dev);
195 return mtd ? mtd_suspend(mtd) : 0;
198 static int mtd_cls_resume(struct device *dev)
200 struct mtd_info *mtd = dev_get_drvdata(dev);
207 static ssize_t mtd_type_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
210 struct mtd_info *mtd = dev_get_drvdata(dev);
235 case MTD_MLCNANDFLASH:
242 return snprintf(buf, PAGE_SIZE, "%s\n", type);
244 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
246 static ssize_t mtd_flags_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 struct mtd_info *mtd = dev_get_drvdata(dev);
251 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
254 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
256 static ssize_t mtd_size_show(struct device *dev,
257 struct device_attribute *attr, char *buf)
259 struct mtd_info *mtd = dev_get_drvdata(dev);
261 return snprintf(buf, PAGE_SIZE, "%llu\n",
262 (unsigned long long)mtd->size);
265 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
267 static ssize_t mtd_erasesize_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 struct mtd_info *mtd = dev_get_drvdata(dev);
272 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
275 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
277 static ssize_t mtd_writesize_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
280 struct mtd_info *mtd = dev_get_drvdata(dev);
282 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
285 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
287 static ssize_t mtd_subpagesize_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 struct mtd_info *mtd = dev_get_drvdata(dev);
291 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
293 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
296 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
298 static ssize_t mtd_oobsize_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
301 struct mtd_info *mtd = dev_get_drvdata(dev);
303 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
306 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
308 static ssize_t mtd_numeraseregions_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 struct mtd_info *mtd = dev_get_drvdata(dev);
313 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
316 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
319 static ssize_t mtd_name_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
322 struct mtd_info *mtd = dev_get_drvdata(dev);
324 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
327 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
329 static ssize_t mtd_ecc_strength_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 struct mtd_info *mtd = dev_get_drvdata(dev);
334 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
336 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
338 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
339 struct device_attribute *attr,
342 struct mtd_info *mtd = dev_get_drvdata(dev);
344 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
347 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf, size_t count)
351 struct mtd_info *mtd = dev_get_drvdata(dev);
352 unsigned int bitflip_threshold;
355 retval = kstrtouint(buf, 0, &bitflip_threshold);
359 mtd->bitflip_threshold = bitflip_threshold;
362 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
363 mtd_bitflip_threshold_show,
364 mtd_bitflip_threshold_store);
366 static ssize_t mtd_ecc_step_size_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct mtd_info *mtd = dev_get_drvdata(dev);
371 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
374 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
376 static struct attribute *mtd_attrs[] = {
378 &dev_attr_flags.attr,
380 &dev_attr_erasesize.attr,
381 &dev_attr_writesize.attr,
382 &dev_attr_subpagesize.attr,
383 &dev_attr_oobsize.attr,
384 &dev_attr_numeraseregions.attr,
386 &dev_attr_ecc_strength.attr,
387 &dev_attr_ecc_step_size.attr,
388 &dev_attr_bitflip_threshold.attr,
391 ATTRIBUTE_GROUPS(mtd);
393 static struct device_type mtd_devtype = {
395 .groups = mtd_groups,
396 .release = mtd_release,
401 * add_mtd_device - register an MTD device
402 * @mtd: pointer to new MTD device info structure
404 * Add a device to the list of MTD devices present in the system, and
405 * notify each currently active MTD 'user' of its arrival. Returns
406 * zero on success or 1 on failure, which currently will only happen
407 * if there is insufficient memory or a sysfs error.
410 int add_mtd_device(struct mtd_info *mtd)
413 struct mtd_notifier *not;
418 if (!mtd->backing_dev_info) {
421 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
424 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
427 mtd->backing_dev_info = &mtd_bdi_unmappable;
433 BUG_ON(mtd->writesize == 0);
434 mutex_lock(&mtd_table_mutex);
436 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
443 INIT_LIST_HEAD(&mtd->partitions);
445 /* default value if not set by driver */
446 if (mtd->bitflip_threshold == 0)
447 mtd->bitflip_threshold = mtd->ecc_strength;
449 if (is_power_of_2(mtd->erasesize))
450 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
452 mtd->erasesize_shift = 0;
454 if (is_power_of_2(mtd->writesize))
455 mtd->writesize_shift = ffs(mtd->writesize) - 1;
457 mtd->writesize_shift = 0;
459 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
460 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
462 /* Some chips always power up locked. Unlock them now */
463 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
464 error = mtd_unlock(mtd, 0, mtd->size);
465 if (error && error != -EOPNOTSUPP)
467 "%s: unlock failed, writes may not work\n",
472 /* Caller should have set dev.parent to match the
475 mtd->dev.type = &mtd_devtype;
476 mtd->dev.class = &mtd_class;
477 mtd->dev.devt = MTD_DEVT(i);
478 dev_set_name(&mtd->dev, "mtd%d", i);
479 dev_set_drvdata(&mtd->dev, mtd);
480 if (device_register(&mtd->dev) != 0)
484 device_create(&mtd_class, mtd->dev.parent,
488 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
489 /* No need to get a refcount on the module containing
490 the notifier, since we hold the mtd_table_mutex */
491 list_for_each_entry(not, &mtd_notifiers, list)
494 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
497 mutex_unlock(&mtd_table_mutex);
498 /* We _know_ we aren't being removed, because
499 our caller is still holding us here. So none
500 of this try_ nonsense, and no bitching about it
502 __module_get(THIS_MODULE);
507 idr_remove(&mtd_idr, i);
510 mutex_unlock(&mtd_table_mutex);
515 * del_mtd_device - unregister an MTD device
516 * @mtd: pointer to MTD device info structure
518 * Remove a device from the list of MTD devices present in the system,
519 * and notify each currently active MTD 'user' of its departure.
520 * Returns zero on success or 1 on failure, which currently will happen
521 * if the requested device does not appear to be present in the list.
524 int del_mtd_device(struct mtd_info *mtd)
528 struct mtd_notifier *not;
531 ret = del_mtd_partitions(mtd);
533 debug("Failed to delete MTD partitions attached to %s (err %d)\n",
538 mutex_lock(&mtd_table_mutex);
540 if (idr_find(&mtd_idr, mtd->index) != mtd) {
546 /* No need to get a refcount on the module containing
547 the notifier, since we hold the mtd_table_mutex */
548 list_for_each_entry(not, &mtd_notifiers, list)
553 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
554 mtd->index, mtd->name, mtd->usecount);
558 device_unregister(&mtd->dev);
561 idr_remove(&mtd_idr, mtd->index);
563 module_put(THIS_MODULE);
568 mutex_unlock(&mtd_table_mutex);
574 * mtd_device_parse_register - parse partitions and register an MTD device.
576 * @mtd: the MTD device to register
577 * @types: the list of MTD partition probes to try, see
578 * 'parse_mtd_partitions()' for more information
579 * @parser_data: MTD partition parser-specific data
580 * @parts: fallback partition information to register, if parsing fails;
581 * only valid if %nr_parts > %0
582 * @nr_parts: the number of partitions in parts, if zero then the full
583 * MTD device is registered if no partition info is found
585 * This function aggregates MTD partitions parsing (done by
586 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
587 * basically follows the most common pattern found in many MTD drivers:
589 * * It first tries to probe partitions on MTD device @mtd using parsers
590 * specified in @types (if @types is %NULL, then the default list of parsers
591 * is used, see 'parse_mtd_partitions()' for more information). If none are
592 * found this functions tries to fallback to information specified in
594 * * If any partitioning info was found, this function registers the found
596 * * If no partitions were found this function just registers the MTD device
599 * Returns zero in case of success and a negative error code in case of failure.
601 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
602 struct mtd_part_parser_data *parser_data,
603 const struct mtd_partition *parts,
607 struct mtd_partition *real_parts;
609 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
610 if (err <= 0 && nr_parts && parts) {
611 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
620 err = add_mtd_partitions(mtd, real_parts, err);
622 } else if (err == 0) {
623 err = add_mtd_device(mtd);
630 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
633 * mtd_device_unregister - unregister an existing MTD device.
635 * @master: the MTD device to unregister. This will unregister both the master
636 * and any partitions if registered.
638 int mtd_device_unregister(struct mtd_info *master)
642 err = del_mtd_partitions(master);
646 if (!device_is_registered(&master->dev))
649 return del_mtd_device(master);
651 EXPORT_SYMBOL_GPL(mtd_device_unregister);
654 * register_mtd_user - register a 'user' of MTD devices.
655 * @new: pointer to notifier info structure
657 * Registers a pair of callbacks function to be called upon addition
658 * or removal of MTD devices. Causes the 'add' callback to be immediately
659 * invoked for each MTD device currently present in the system.
661 void register_mtd_user (struct mtd_notifier *new)
663 struct mtd_info *mtd;
665 mutex_lock(&mtd_table_mutex);
667 list_add(&new->list, &mtd_notifiers);
669 __module_get(THIS_MODULE);
671 mtd_for_each_device(mtd)
674 mutex_unlock(&mtd_table_mutex);
676 EXPORT_SYMBOL_GPL(register_mtd_user);
679 * unregister_mtd_user - unregister a 'user' of MTD devices.
680 * @old: pointer to notifier info structure
682 * Removes a callback function pair from the list of 'users' to be
683 * notified upon addition or removal of MTD devices. Causes the
684 * 'remove' callback to be immediately invoked for each MTD device
685 * currently present in the system.
687 int unregister_mtd_user (struct mtd_notifier *old)
689 struct mtd_info *mtd;
691 mutex_lock(&mtd_table_mutex);
693 module_put(THIS_MODULE);
695 mtd_for_each_device(mtd)
698 list_del(&old->list);
699 mutex_unlock(&mtd_table_mutex);
702 EXPORT_SYMBOL_GPL(unregister_mtd_user);
706 * get_mtd_device - obtain a validated handle for an MTD device
707 * @mtd: last known address of the required MTD device
708 * @num: internal device number of the required MTD device
710 * Given a number and NULL address, return the num'th entry in the device
711 * table, if any. Given an address and num == -1, search the device table
712 * for a device with that address and return if it's still present. Given
713 * both, return the num'th driver only if its address matches. Return
716 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
718 struct mtd_info *ret = NULL, *other;
721 mutex_lock(&mtd_table_mutex);
724 mtd_for_each_device(other) {
730 } else if (num >= 0) {
731 ret = idr_find(&mtd_idr, num);
732 if (mtd && mtd != ret)
741 err = __get_mtd_device(ret);
745 mutex_unlock(&mtd_table_mutex);
748 EXPORT_SYMBOL_GPL(get_mtd_device);
751 int __get_mtd_device(struct mtd_info *mtd)
755 if (!try_module_get(mtd->owner))
758 if (mtd->_get_device) {
759 err = mtd->_get_device(mtd);
762 module_put(mtd->owner);
769 EXPORT_SYMBOL_GPL(__get_mtd_device);
772 * get_mtd_device_nm - obtain a validated handle for an MTD device by
774 * @name: MTD device name to open
776 * This function returns MTD device description structure in case of
777 * success and an error code in case of failure.
779 struct mtd_info *get_mtd_device_nm(const char *name)
782 struct mtd_info *mtd = NULL, *other;
784 mutex_lock(&mtd_table_mutex);
786 mtd_for_each_device(other) {
787 if (!strcmp(name, other->name)) {
796 err = __get_mtd_device(mtd);
800 mutex_unlock(&mtd_table_mutex);
804 mutex_unlock(&mtd_table_mutex);
807 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
809 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
811 * mtd_get_len_incl_bad
813 * Check if length including bad blocks fits into device.
815 * @param mtd an MTD device
816 * @param offset offset in flash
817 * @param length image length
818 * @return image length including bad blocks in *len_incl_bad and whether or not
819 * the length returned was truncated in *truncated
821 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
822 const uint64_t length, uint64_t *len_incl_bad,
828 if (!mtd->_block_isbad) {
829 *len_incl_bad = length;
833 uint64_t len_excl_bad = 0;
836 while (len_excl_bad < length) {
837 if (offset >= mtd->size) {
842 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
844 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
845 len_excl_bad += block_len;
847 *len_incl_bad += block_len;
851 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
853 void put_mtd_device(struct mtd_info *mtd)
855 mutex_lock(&mtd_table_mutex);
856 __put_mtd_device(mtd);
857 mutex_unlock(&mtd_table_mutex);
860 EXPORT_SYMBOL_GPL(put_mtd_device);
862 void __put_mtd_device(struct mtd_info *mtd)
865 BUG_ON(mtd->usecount < 0);
867 if (mtd->_put_device)
868 mtd->_put_device(mtd);
870 module_put(mtd->owner);
872 EXPORT_SYMBOL_GPL(__put_mtd_device);
875 * Erase is an asynchronous operation. Device drivers are supposed
876 * to call instr->callback() whenever the operation completes, even
877 * if it completes with a failure.
878 * Callers are supposed to pass a callback function and wait for it
879 * to be called before writing to the block.
881 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
883 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
885 if (!(mtd->flags & MTD_WRITEABLE))
887 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
889 instr->state = MTD_ERASE_DONE;
890 mtd_erase_callback(instr);
893 return mtd->_erase(mtd, instr);
895 EXPORT_SYMBOL_GPL(mtd_erase);
899 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
901 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
902 void **virt, resource_size_t *phys)
910 if (from < 0 || from > mtd->size || len > mtd->size - from)
914 return mtd->_point(mtd, from, len, retlen, virt, phys);
916 EXPORT_SYMBOL_GPL(mtd_point);
918 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
919 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
923 if (from < 0 || from > mtd->size || len > mtd->size - from)
927 return mtd->_unpoint(mtd, from, len);
929 EXPORT_SYMBOL_GPL(mtd_unpoint);
933 * Allow NOMMU mmap() to directly map the device (if not NULL)
934 * - return the address to which the offset maps
935 * - return -ENOSYS to indicate refusal to do the mapping
937 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
938 unsigned long offset, unsigned long flags)
940 if (!mtd->_get_unmapped_area)
942 if (offset > mtd->size || len > mtd->size - offset)
944 return mtd->_get_unmapped_area(mtd, len, offset, flags);
946 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
948 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
953 if (from < 0 || from > mtd->size || len > mtd->size - from)
959 * In the absence of an error, drivers return a non-negative integer
960 * representing the maximum number of bitflips that were corrected on
961 * any one ecc region (if applicable; zero otherwise).
964 ret_code = mtd->_read(mtd, from, len, retlen, buf);
965 } else if (mtd->_read_oob) {
966 struct mtd_oob_ops ops = {
971 ret_code = mtd->_read_oob(mtd, from, &ops);
972 *retlen = ops.retlen;
977 if (unlikely(ret_code < 0))
979 if (mtd->ecc_strength == 0)
980 return 0; /* device lacks ecc */
981 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
983 EXPORT_SYMBOL_GPL(mtd_read);
985 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
989 if (to < 0 || to > mtd->size || len > mtd->size - to)
991 if ((!mtd->_write && !mtd->_write_oob) ||
992 !(mtd->flags & MTD_WRITEABLE))
998 struct mtd_oob_ops ops = {
1000 .datbuf = (u8 *)buf,
1004 ret = mtd->_write_oob(mtd, to, &ops);
1005 *retlen = ops.retlen;
1009 return mtd->_write(mtd, to, len, retlen, buf);
1011 EXPORT_SYMBOL_GPL(mtd_write);
1014 * In blackbox flight recorder like scenarios we want to make successful writes
1015 * in interrupt context. panic_write() is only intended to be called when its
1016 * known the kernel is about to panic and we need the write to succeed. Since
1017 * the kernel is not going to be running for much longer, this function can
1018 * break locks and delay to ensure the write succeeds (but not sleep).
1020 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1024 if (!mtd->_panic_write)
1026 if (to < 0 || to > mtd->size || len > mtd->size - to)
1028 if (!(mtd->flags & MTD_WRITEABLE))
1032 return mtd->_panic_write(mtd, to, len, retlen, buf);
1034 EXPORT_SYMBOL_GPL(mtd_panic_write);
1036 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1037 struct mtd_oob_ops *ops)
1040 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1041 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1050 if (offs < 0 || offs + ops->len > mtd->size)
1056 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1059 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
1060 mtd_div_by_ws(offs, mtd)) *
1061 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1062 if (ops->ooblen > maxooblen)
1069 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1072 ops->retlen = ops->oobretlen = 0;
1074 ret_code = mtd_check_oob_ops(mtd, from, ops);
1078 /* Check the validity of a potential fallback on mtd->_read */
1079 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1083 ret_code = mtd->_read_oob(mtd, from, ops);
1085 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1089 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1090 * similar to mtd->_read(), returning a non-negative integer
1091 * representing max bitflips. In other cases, mtd->_read_oob() may
1092 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1094 if (unlikely(ret_code < 0))
1096 if (mtd->ecc_strength == 0)
1097 return 0; /* device lacks ecc */
1098 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1100 EXPORT_SYMBOL_GPL(mtd_read_oob);
1102 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1103 struct mtd_oob_ops *ops)
1107 ops->retlen = ops->oobretlen = 0;
1109 if (!(mtd->flags & MTD_WRITEABLE))
1112 ret = mtd_check_oob_ops(mtd, to, ops);
1116 /* Check the validity of a potential fallback on mtd->_write */
1117 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1120 if (mtd->_write_oob)
1121 return mtd->_write_oob(mtd, to, ops);
1123 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1126 EXPORT_SYMBOL_GPL(mtd_write_oob);
1129 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1130 * @mtd: MTD device structure
1131 * @section: ECC section. Depending on the layout you may have all the ECC
1132 * bytes stored in a single contiguous section, or one section
1133 * per ECC chunk (and sometime several sections for a single ECC
1135 * @oobecc: OOB region struct filled with the appropriate ECC position
1138 * This function returns ECC section information in the OOB area. If you want
1139 * to get all the ECC bytes information, then you should call
1140 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1142 * Returns zero on success, a negative error code otherwise.
1144 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1145 struct mtd_oob_region *oobecc)
1147 memset(oobecc, 0, sizeof(*oobecc));
1149 if (!mtd || section < 0)
1152 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1155 return mtd->ooblayout->ecc(mtd, section, oobecc);
1157 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1160 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1162 * @mtd: MTD device structure
1163 * @section: Free section you are interested in. Depending on the layout
1164 * you may have all the free bytes stored in a single contiguous
1165 * section, or one section per ECC chunk plus an extra section
1166 * for the remaining bytes (or other funky layout).
1167 * @oobfree: OOB region struct filled with the appropriate free position
1170 * This function returns free bytes position in the OOB area. If you want
1171 * to get all the free bytes information, then you should call
1172 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1174 * Returns zero on success, a negative error code otherwise.
1176 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1177 struct mtd_oob_region *oobfree)
1179 memset(oobfree, 0, sizeof(*oobfree));
1181 if (!mtd || section < 0)
1184 if (!mtd->ooblayout || !mtd->ooblayout->free)
1187 return mtd->ooblayout->free(mtd, section, oobfree);
1189 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1192 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1193 * @mtd: mtd info structure
1194 * @byte: the byte we are searching for
1195 * @sectionp: pointer where the section id will be stored
1196 * @oobregion: used to retrieve the ECC position
1197 * @iter: iterator function. Should be either mtd_ooblayout_free or
1198 * mtd_ooblayout_ecc depending on the region type you're searching for
1200 * This function returns the section id and oobregion information of a
1201 * specific byte. For example, say you want to know where the 4th ECC byte is
1202 * stored, you'll use:
1204 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1206 * Returns zero on success, a negative error code otherwise.
1208 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1209 int *sectionp, struct mtd_oob_region *oobregion,
1210 int (*iter)(struct mtd_info *,
1212 struct mtd_oob_region *oobregion))
1214 int pos = 0, ret, section = 0;
1216 memset(oobregion, 0, sizeof(*oobregion));
1219 ret = iter(mtd, section, oobregion);
1223 if (pos + oobregion->length > byte)
1226 pos += oobregion->length;
1231 * Adjust region info to make it start at the beginning at the
1234 oobregion->offset += byte - pos;
1235 oobregion->length -= byte - pos;
1236 *sectionp = section;
1242 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1244 * @mtd: mtd info structure
1245 * @eccbyte: the byte we are searching for
1246 * @sectionp: pointer where the section id will be stored
1247 * @oobregion: OOB region information
1249 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1252 * Returns zero on success, a negative error code otherwise.
1254 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1256 struct mtd_oob_region *oobregion)
1258 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1261 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1264 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1265 * @mtd: mtd info structure
1266 * @buf: destination buffer to store OOB bytes
1267 * @oobbuf: OOB buffer
1268 * @start: first byte to retrieve
1269 * @nbytes: number of bytes to retrieve
1270 * @iter: section iterator
1272 * Extract bytes attached to a specific category (ECC or free)
1273 * from the OOB buffer and copy them into buf.
1275 * Returns zero on success, a negative error code otherwise.
1277 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1278 const u8 *oobbuf, int start, int nbytes,
1279 int (*iter)(struct mtd_info *,
1281 struct mtd_oob_region *oobregion))
1283 struct mtd_oob_region oobregion;
1286 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1292 cnt = min_t(int, nbytes, oobregion.length);
1293 memcpy(buf, oobbuf + oobregion.offset, cnt);
1300 ret = iter(mtd, ++section, &oobregion);
1307 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1308 * @mtd: mtd info structure
1309 * @buf: source buffer to get OOB bytes from
1310 * @oobbuf: OOB buffer
1311 * @start: first OOB byte to set
1312 * @nbytes: number of OOB bytes to set
1313 * @iter: section iterator
1315 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1316 * is selected by passing the appropriate iterator.
1318 * Returns zero on success, a negative error code otherwise.
1320 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1321 u8 *oobbuf, int start, int nbytes,
1322 int (*iter)(struct mtd_info *,
1324 struct mtd_oob_region *oobregion))
1326 struct mtd_oob_region oobregion;
1329 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1335 cnt = min_t(int, nbytes, oobregion.length);
1336 memcpy(oobbuf + oobregion.offset, buf, cnt);
1343 ret = iter(mtd, ++section, &oobregion);
1350 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1351 * @mtd: mtd info structure
1352 * @iter: category iterator
1354 * Count the number of bytes in a given category.
1356 * Returns a positive value on success, a negative error code otherwise.
1358 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1359 int (*iter)(struct mtd_info *,
1361 struct mtd_oob_region *oobregion))
1363 struct mtd_oob_region oobregion;
1364 int section = 0, ret, nbytes = 0;
1367 ret = iter(mtd, section++, &oobregion);
1374 nbytes += oobregion.length;
1381 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1382 * @mtd: mtd info structure
1383 * @eccbuf: destination buffer to store ECC bytes
1384 * @oobbuf: OOB buffer
1385 * @start: first ECC byte to retrieve
1386 * @nbytes: number of ECC bytes to retrieve
1388 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1390 * Returns zero on success, a negative error code otherwise.
1392 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1393 const u8 *oobbuf, int start, int nbytes)
1395 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1398 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1401 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1402 * @mtd: mtd info structure
1403 * @eccbuf: source buffer to get ECC bytes from
1404 * @oobbuf: OOB buffer
1405 * @start: first ECC byte to set
1406 * @nbytes: number of ECC bytes to set
1408 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1410 * Returns zero on success, a negative error code otherwise.
1412 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1413 u8 *oobbuf, int start, int nbytes)
1415 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1418 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1421 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1422 * @mtd: mtd info structure
1423 * @databuf: destination buffer to store ECC bytes
1424 * @oobbuf: OOB buffer
1425 * @start: first ECC byte to retrieve
1426 * @nbytes: number of ECC bytes to retrieve
1428 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1430 * Returns zero on success, a negative error code otherwise.
1432 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1433 const u8 *oobbuf, int start, int nbytes)
1435 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1436 mtd_ooblayout_free);
1438 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1441 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1442 * @mtd: mtd info structure
1443 * @eccbuf: source buffer to get data bytes from
1444 * @oobbuf: OOB buffer
1445 * @start: first ECC byte to set
1446 * @nbytes: number of ECC bytes to set
1448 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1450 * Returns zero on success, a negative error code otherwise.
1452 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1453 u8 *oobbuf, int start, int nbytes)
1455 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1456 mtd_ooblayout_free);
1458 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1461 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1462 * @mtd: mtd info structure
1464 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1466 * Returns zero on success, a negative error code otherwise.
1468 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1470 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1472 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1475 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1476 * @mtd: mtd info structure
1478 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1480 * Returns zero on success, a negative error code otherwise.
1482 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1484 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1486 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1489 * Method to access the protection register area, present in some flash
1490 * devices. The user data is one time programmable but the factory data is read
1493 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1494 struct otp_info *buf)
1496 if (!mtd->_get_fact_prot_info)
1500 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1502 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1504 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1505 size_t *retlen, u_char *buf)
1508 if (!mtd->_read_fact_prot_reg)
1512 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1514 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1516 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1517 struct otp_info *buf)
1519 if (!mtd->_get_user_prot_info)
1523 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1525 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1527 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1528 size_t *retlen, u_char *buf)
1531 if (!mtd->_read_user_prot_reg)
1535 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1537 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1539 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1540 size_t *retlen, u_char *buf)
1545 if (!mtd->_write_user_prot_reg)
1549 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1554 * If no data could be written at all, we are out of memory and
1555 * must return -ENOSPC.
1557 return (*retlen) ? 0 : -ENOSPC;
1559 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1561 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1563 if (!mtd->_lock_user_prot_reg)
1567 return mtd->_lock_user_prot_reg(mtd, from, len);
1569 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1571 /* Chip-supported device locking */
1572 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1576 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1580 return mtd->_lock(mtd, ofs, len);
1582 EXPORT_SYMBOL_GPL(mtd_lock);
1584 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1588 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1592 return mtd->_unlock(mtd, ofs, len);
1594 EXPORT_SYMBOL_GPL(mtd_unlock);
1596 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1598 if (!mtd->_is_locked)
1600 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1604 return mtd->_is_locked(mtd, ofs, len);
1606 EXPORT_SYMBOL_GPL(mtd_is_locked);
1608 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1610 if (ofs < 0 || ofs > mtd->size)
1612 if (!mtd->_block_isreserved)
1614 return mtd->_block_isreserved(mtd, ofs);
1616 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1618 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1620 if (ofs < 0 || ofs > mtd->size)
1622 if (!mtd->_block_isbad)
1624 return mtd->_block_isbad(mtd, ofs);
1626 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1628 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1630 if (!mtd->_block_markbad)
1632 if (ofs < 0 || ofs > mtd->size)
1634 if (!(mtd->flags & MTD_WRITEABLE))
1636 return mtd->_block_markbad(mtd, ofs);
1638 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1642 * default_mtd_writev - the default writev method
1643 * @mtd: mtd device description object pointer
1644 * @vecs: the vectors to write
1645 * @count: count of vectors in @vecs
1646 * @to: the MTD device offset to write to
1647 * @retlen: on exit contains the count of bytes written to the MTD device.
1649 * This function returns zero in case of success and a negative error code in
1652 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1653 unsigned long count, loff_t to, size_t *retlen)
1656 size_t totlen = 0, thislen;
1659 for (i = 0; i < count; i++) {
1660 if (!vecs[i].iov_len)
1662 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1665 if (ret || thislen != vecs[i].iov_len)
1667 to += vecs[i].iov_len;
1674 * mtd_writev - the vector-based MTD write method
1675 * @mtd: mtd device description object pointer
1676 * @vecs: the vectors to write
1677 * @count: count of vectors in @vecs
1678 * @to: the MTD device offset to write to
1679 * @retlen: on exit contains the count of bytes written to the MTD device.
1681 * This function returns zero in case of success and a negative error code in
1684 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1685 unsigned long count, loff_t to, size_t *retlen)
1688 if (!(mtd->flags & MTD_WRITEABLE))
1691 return default_mtd_writev(mtd, vecs, count, to, retlen);
1692 return mtd->_writev(mtd, vecs, count, to, retlen);
1694 EXPORT_SYMBOL_GPL(mtd_writev);
1697 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1698 * @mtd: mtd device description object pointer
1699 * @size: a pointer to the ideal or maximum size of the allocation, points
1700 * to the actual allocation size on success.
1702 * This routine attempts to allocate a contiguous kernel buffer up to
1703 * the specified size, backing off the size of the request exponentially
1704 * until the request succeeds or until the allocation size falls below
1705 * the system page size. This attempts to make sure it does not adversely
1706 * impact system performance, so when allocating more than one page, we
1707 * ask the memory allocator to avoid re-trying, swapping, writing back
1708 * or performing I/O.
1710 * Note, this function also makes sure that the allocated buffer is aligned to
1711 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1713 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1714 * to handle smaller (i.e. degraded) buffer allocations under low- or
1715 * fragmented-memory situations where such reduced allocations, from a
1716 * requested ideal, are allowed.
1718 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1720 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1722 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1723 __GFP_NORETRY | __GFP_NO_KSWAPD;
1724 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1727 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1729 while (*size > min_alloc) {
1730 kbuf = kmalloc(*size, flags);
1735 *size = ALIGN(*size, mtd->writesize);
1739 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1740 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1742 return kmalloc(*size, GFP_KERNEL);
1744 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1747 #ifdef CONFIG_PROC_FS
1749 /*====================================================================*/
1750 /* Support for /proc/mtd */
1752 static int mtd_proc_show(struct seq_file *m, void *v)
1754 struct mtd_info *mtd;
1756 seq_puts(m, "dev: size erasesize name\n");
1757 mutex_lock(&mtd_table_mutex);
1758 mtd_for_each_device(mtd) {
1759 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1760 mtd->index, (unsigned long long)mtd->size,
1761 mtd->erasesize, mtd->name);
1763 mutex_unlock(&mtd_table_mutex);
1767 static int mtd_proc_open(struct inode *inode, struct file *file)
1769 return single_open(file, mtd_proc_show, NULL);
1772 static const struct file_operations mtd_proc_ops = {
1773 .open = mtd_proc_open,
1775 .llseek = seq_lseek,
1776 .release = single_release,
1778 #endif /* CONFIG_PROC_FS */
1780 /*====================================================================*/
1784 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1788 ret = bdi_init(bdi);
1790 ret = bdi_register(bdi, NULL, "%s", name);
1798 static struct proc_dir_entry *proc_mtd;
1800 static int __init init_mtd(void)
1804 ret = class_register(&mtd_class);
1808 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1812 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1816 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1820 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1822 ret = init_mtdchar();
1830 remove_proc_entry("mtd", NULL);
1832 bdi_destroy(&mtd_bdi_ro_mappable);
1834 bdi_destroy(&mtd_bdi_unmappable);
1836 class_unregister(&mtd_class);
1838 pr_err("Error registering mtd class or bdi: %d\n", ret);
1842 static void __exit cleanup_mtd(void)
1846 remove_proc_entry("mtd", NULL);
1847 class_unregister(&mtd_class);
1848 bdi_destroy(&mtd_bdi_unmappable);
1849 bdi_destroy(&mtd_bdi_ro_mappable);
1850 bdi_destroy(&mtd_bdi_rw_mappable);
1853 module_init(init_mtd);
1854 module_exit(cleanup_mtd);
1857 MODULE_LICENSE("GPL");
1858 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1859 MODULE_DESCRIPTION("Core MTD registration and access routines");