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];
92 #define DEFINE_IDR(name) struct idr name;
94 void idr_remove(struct idr *idp, int id)
101 void *idr_find(struct idr *idp, int id)
103 if (idp->id[id].used)
104 return idp->id[id].ptr;
109 void *idr_get_next(struct idr *idp, int *next)
114 ret = idr_find(idp, id);
117 if (!idp->id[id].used)
127 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
129 struct idr_layer *idl;
132 while (i < MAX_IDR_ID) {
134 if (idl->used == 0) {
145 static DEFINE_IDR(mtd_idr);
147 /* These are exported solely for the purpose of mtd_blkdevs.c. You
148 should not use them for _anything_ else */
149 DEFINE_MUTEX(mtd_table_mutex);
150 EXPORT_SYMBOL_GPL(mtd_table_mutex);
152 struct mtd_info *__mtd_next_device(int i)
154 return idr_get_next(&mtd_idr, &i);
156 EXPORT_SYMBOL_GPL(__mtd_next_device);
159 static LIST_HEAD(mtd_notifiers);
162 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
164 /* REVISIT once MTD uses the driver model better, whoever allocates
165 * the mtd_info will probably want to use the release() hook...
167 static void mtd_release(struct device *dev)
169 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
170 dev_t index = MTD_DEVT(mtd->index);
172 /* remove /dev/mtdXro node if needed */
174 device_destroy(&mtd_class, index + 1);
177 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
179 struct mtd_info *mtd = dev_get_drvdata(dev);
181 return mtd ? mtd_suspend(mtd) : 0;
184 static int mtd_cls_resume(struct device *dev)
186 struct mtd_info *mtd = dev_get_drvdata(dev);
193 static ssize_t mtd_type_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 struct mtd_info *mtd = dev_get_drvdata(dev);
221 case MTD_MLCNANDFLASH:
228 return snprintf(buf, PAGE_SIZE, "%s\n", type);
230 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
232 static ssize_t mtd_flags_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct mtd_info *mtd = dev_get_drvdata(dev);
237 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
240 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
242 static ssize_t mtd_size_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
245 struct mtd_info *mtd = dev_get_drvdata(dev);
247 return snprintf(buf, PAGE_SIZE, "%llu\n",
248 (unsigned long long)mtd->size);
251 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
253 static ssize_t mtd_erasesize_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
256 struct mtd_info *mtd = dev_get_drvdata(dev);
258 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
261 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
263 static ssize_t mtd_writesize_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
266 struct mtd_info *mtd = dev_get_drvdata(dev);
268 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
271 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
273 static ssize_t mtd_subpagesize_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
276 struct mtd_info *mtd = dev_get_drvdata(dev);
277 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
279 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
282 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
284 static ssize_t mtd_oobsize_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct mtd_info *mtd = dev_get_drvdata(dev);
289 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
292 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
294 static ssize_t mtd_numeraseregions_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
297 struct mtd_info *mtd = dev_get_drvdata(dev);
299 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
302 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
305 static ssize_t mtd_name_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct mtd_info *mtd = dev_get_drvdata(dev);
310 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
313 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
315 static ssize_t mtd_ecc_strength_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct mtd_info *mtd = dev_get_drvdata(dev);
320 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
322 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
324 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
325 struct device_attribute *attr,
328 struct mtd_info *mtd = dev_get_drvdata(dev);
330 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
333 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf, size_t count)
337 struct mtd_info *mtd = dev_get_drvdata(dev);
338 unsigned int bitflip_threshold;
341 retval = kstrtouint(buf, 0, &bitflip_threshold);
345 mtd->bitflip_threshold = bitflip_threshold;
348 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
349 mtd_bitflip_threshold_show,
350 mtd_bitflip_threshold_store);
352 static ssize_t mtd_ecc_step_size_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct mtd_info *mtd = dev_get_drvdata(dev);
357 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
360 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
362 static struct attribute *mtd_attrs[] = {
364 &dev_attr_flags.attr,
366 &dev_attr_erasesize.attr,
367 &dev_attr_writesize.attr,
368 &dev_attr_subpagesize.attr,
369 &dev_attr_oobsize.attr,
370 &dev_attr_numeraseregions.attr,
372 &dev_attr_ecc_strength.attr,
373 &dev_attr_ecc_step_size.attr,
374 &dev_attr_bitflip_threshold.attr,
377 ATTRIBUTE_GROUPS(mtd);
379 static struct device_type mtd_devtype = {
381 .groups = mtd_groups,
382 .release = mtd_release,
387 * add_mtd_device - register an MTD device
388 * @mtd: pointer to new MTD device info structure
390 * Add a device to the list of MTD devices present in the system, and
391 * notify each currently active MTD 'user' of its arrival. Returns
392 * zero on success or 1 on failure, which currently will only happen
393 * if there is insufficient memory or a sysfs error.
396 int add_mtd_device(struct mtd_info *mtd)
399 struct mtd_notifier *not;
404 if (!mtd->backing_dev_info) {
407 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
410 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
413 mtd->backing_dev_info = &mtd_bdi_unmappable;
419 BUG_ON(mtd->writesize == 0);
420 mutex_lock(&mtd_table_mutex);
422 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
429 INIT_LIST_HEAD(&mtd->partitions);
431 /* default value if not set by driver */
432 if (mtd->bitflip_threshold == 0)
433 mtd->bitflip_threshold = mtd->ecc_strength;
435 if (is_power_of_2(mtd->erasesize))
436 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
438 mtd->erasesize_shift = 0;
440 if (is_power_of_2(mtd->writesize))
441 mtd->writesize_shift = ffs(mtd->writesize) - 1;
443 mtd->writesize_shift = 0;
445 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
446 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
448 /* Some chips always power up locked. Unlock them now */
449 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
450 error = mtd_unlock(mtd, 0, mtd->size);
451 if (error && error != -EOPNOTSUPP)
453 "%s: unlock failed, writes may not work\n",
458 /* Caller should have set dev.parent to match the
461 mtd->dev.type = &mtd_devtype;
462 mtd->dev.class = &mtd_class;
463 mtd->dev.devt = MTD_DEVT(i);
464 dev_set_name(&mtd->dev, "mtd%d", i);
465 dev_set_drvdata(&mtd->dev, mtd);
466 if (device_register(&mtd->dev) != 0)
470 device_create(&mtd_class, mtd->dev.parent,
474 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
475 /* No need to get a refcount on the module containing
476 the notifier, since we hold the mtd_table_mutex */
477 list_for_each_entry(not, &mtd_notifiers, list)
480 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
483 mutex_unlock(&mtd_table_mutex);
484 /* We _know_ we aren't being removed, because
485 our caller is still holding us here. So none
486 of this try_ nonsense, and no bitching about it
488 __module_get(THIS_MODULE);
493 idr_remove(&mtd_idr, i);
496 mutex_unlock(&mtd_table_mutex);
501 * del_mtd_device - unregister an MTD device
502 * @mtd: pointer to MTD device info structure
504 * Remove a device from the list of MTD devices present in the system,
505 * and notify each currently active MTD 'user' of its departure.
506 * Returns zero on success or 1 on failure, which currently will happen
507 * if the requested device does not appear to be present in the list.
510 int del_mtd_device(struct mtd_info *mtd)
514 struct mtd_notifier *not;
517 mutex_lock(&mtd_table_mutex);
519 if (idr_find(&mtd_idr, mtd->index) != mtd) {
525 /* No need to get a refcount on the module containing
526 the notifier, since we hold the mtd_table_mutex */
527 list_for_each_entry(not, &mtd_notifiers, list)
532 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
533 mtd->index, mtd->name, mtd->usecount);
537 device_unregister(&mtd->dev);
540 idr_remove(&mtd_idr, mtd->index);
542 module_put(THIS_MODULE);
547 mutex_unlock(&mtd_table_mutex);
553 * mtd_device_parse_register - parse partitions and register an MTD device.
555 * @mtd: the MTD device to register
556 * @types: the list of MTD partition probes to try, see
557 * 'parse_mtd_partitions()' for more information
558 * @parser_data: MTD partition parser-specific data
559 * @parts: fallback partition information to register, if parsing fails;
560 * only valid if %nr_parts > %0
561 * @nr_parts: the number of partitions in parts, if zero then the full
562 * MTD device is registered if no partition info is found
564 * This function aggregates MTD partitions parsing (done by
565 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
566 * basically follows the most common pattern found in many MTD drivers:
568 * * It first tries to probe partitions on MTD device @mtd using parsers
569 * specified in @types (if @types is %NULL, then the default list of parsers
570 * is used, see 'parse_mtd_partitions()' for more information). If none are
571 * found this functions tries to fallback to information specified in
573 * * If any partitioning info was found, this function registers the found
575 * * If no partitions were found this function just registers the MTD device
578 * Returns zero in case of success and a negative error code in case of failure.
580 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
581 struct mtd_part_parser_data *parser_data,
582 const struct mtd_partition *parts,
586 struct mtd_partition *real_parts;
588 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
589 if (err <= 0 && nr_parts && parts) {
590 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
599 err = add_mtd_partitions(mtd, real_parts, err);
601 } else if (err == 0) {
602 err = add_mtd_device(mtd);
609 EXPORT_SYMBOL_GPL(mtd_device_parse_register);
612 * mtd_device_unregister - unregister an existing MTD device.
614 * @master: the MTD device to unregister. This will unregister both the master
615 * and any partitions if registered.
617 int mtd_device_unregister(struct mtd_info *master)
621 err = del_mtd_partitions(master);
625 if (!device_is_registered(&master->dev))
628 return del_mtd_device(master);
630 EXPORT_SYMBOL_GPL(mtd_device_unregister);
633 * register_mtd_user - register a 'user' of MTD devices.
634 * @new: pointer to notifier info structure
636 * Registers a pair of callbacks function to be called upon addition
637 * or removal of MTD devices. Causes the 'add' callback to be immediately
638 * invoked for each MTD device currently present in the system.
640 void register_mtd_user (struct mtd_notifier *new)
642 struct mtd_info *mtd;
644 mutex_lock(&mtd_table_mutex);
646 list_add(&new->list, &mtd_notifiers);
648 __module_get(THIS_MODULE);
650 mtd_for_each_device(mtd)
653 mutex_unlock(&mtd_table_mutex);
655 EXPORT_SYMBOL_GPL(register_mtd_user);
658 * unregister_mtd_user - unregister a 'user' of MTD devices.
659 * @old: pointer to notifier info structure
661 * Removes a callback function pair from the list of 'users' to be
662 * notified upon addition or removal of MTD devices. Causes the
663 * 'remove' callback to be immediately invoked for each MTD device
664 * currently present in the system.
666 int unregister_mtd_user (struct mtd_notifier *old)
668 struct mtd_info *mtd;
670 mutex_lock(&mtd_table_mutex);
672 module_put(THIS_MODULE);
674 mtd_for_each_device(mtd)
677 list_del(&old->list);
678 mutex_unlock(&mtd_table_mutex);
681 EXPORT_SYMBOL_GPL(unregister_mtd_user);
685 * get_mtd_device - obtain a validated handle for an MTD device
686 * @mtd: last known address of the required MTD device
687 * @num: internal device number of the required MTD device
689 * Given a number and NULL address, return the num'th entry in the device
690 * table, if any. Given an address and num == -1, search the device table
691 * for a device with that address and return if it's still present. Given
692 * both, return the num'th driver only if its address matches. Return
695 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
697 struct mtd_info *ret = NULL, *other;
700 mutex_lock(&mtd_table_mutex);
703 mtd_for_each_device(other) {
709 } else if (num >= 0) {
710 ret = idr_find(&mtd_idr, num);
711 if (mtd && mtd != ret)
720 err = __get_mtd_device(ret);
724 mutex_unlock(&mtd_table_mutex);
727 EXPORT_SYMBOL_GPL(get_mtd_device);
730 int __get_mtd_device(struct mtd_info *mtd)
734 if (!try_module_get(mtd->owner))
737 if (mtd->_get_device) {
738 err = mtd->_get_device(mtd);
741 module_put(mtd->owner);
748 EXPORT_SYMBOL_GPL(__get_mtd_device);
751 * get_mtd_device_nm - obtain a validated handle for an MTD device by
753 * @name: MTD device name to open
755 * This function returns MTD device description structure in case of
756 * success and an error code in case of failure.
758 struct mtd_info *get_mtd_device_nm(const char *name)
761 struct mtd_info *mtd = NULL, *other;
763 mutex_lock(&mtd_table_mutex);
765 mtd_for_each_device(other) {
766 if (!strcmp(name, other->name)) {
775 err = __get_mtd_device(mtd);
779 mutex_unlock(&mtd_table_mutex);
783 mutex_unlock(&mtd_table_mutex);
786 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
788 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
790 * mtd_get_len_incl_bad
792 * Check if length including bad blocks fits into device.
794 * @param mtd an MTD device
795 * @param offset offset in flash
796 * @param length image length
797 * @return image length including bad blocks in *len_incl_bad and whether or not
798 * the length returned was truncated in *truncated
800 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
801 const uint64_t length, uint64_t *len_incl_bad,
807 if (!mtd->_block_isbad) {
808 *len_incl_bad = length;
812 uint64_t len_excl_bad = 0;
815 while (len_excl_bad < length) {
816 if (offset >= mtd->size) {
821 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
823 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
824 len_excl_bad += block_len;
826 *len_incl_bad += block_len;
830 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
832 void put_mtd_device(struct mtd_info *mtd)
834 mutex_lock(&mtd_table_mutex);
835 __put_mtd_device(mtd);
836 mutex_unlock(&mtd_table_mutex);
839 EXPORT_SYMBOL_GPL(put_mtd_device);
841 void __put_mtd_device(struct mtd_info *mtd)
844 BUG_ON(mtd->usecount < 0);
846 if (mtd->_put_device)
847 mtd->_put_device(mtd);
849 module_put(mtd->owner);
851 EXPORT_SYMBOL_GPL(__put_mtd_device);
854 * Erase is an asynchronous operation. Device drivers are supposed
855 * to call instr->callback() whenever the operation completes, even
856 * if it completes with a failure.
857 * Callers are supposed to pass a callback function and wait for it
858 * to be called before writing to the block.
860 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
862 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
864 if (!(mtd->flags & MTD_WRITEABLE))
866 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
868 instr->state = MTD_ERASE_DONE;
869 mtd_erase_callback(instr);
872 return mtd->_erase(mtd, instr);
874 EXPORT_SYMBOL_GPL(mtd_erase);
878 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
880 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
881 void **virt, resource_size_t *phys)
889 if (from < 0 || from > mtd->size || len > mtd->size - from)
893 return mtd->_point(mtd, from, len, retlen, virt, phys);
895 EXPORT_SYMBOL_GPL(mtd_point);
897 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
898 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
902 if (from < 0 || from > mtd->size || len > mtd->size - from)
906 return mtd->_unpoint(mtd, from, len);
908 EXPORT_SYMBOL_GPL(mtd_unpoint);
912 * Allow NOMMU mmap() to directly map the device (if not NULL)
913 * - return the address to which the offset maps
914 * - return -ENOSYS to indicate refusal to do the mapping
916 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
917 unsigned long offset, unsigned long flags)
919 if (!mtd->_get_unmapped_area)
921 if (offset > mtd->size || len > mtd->size - offset)
923 return mtd->_get_unmapped_area(mtd, len, offset, flags);
925 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
927 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
932 if (from < 0 || from > mtd->size || len > mtd->size - from)
938 * In the absence of an error, drivers return a non-negative integer
939 * representing the maximum number of bitflips that were corrected on
940 * any one ecc region (if applicable; zero otherwise).
943 ret_code = mtd->_read(mtd, from, len, retlen, buf);
944 } else if (mtd->_read_oob) {
945 struct mtd_oob_ops ops = {
950 ret_code = mtd->_read_oob(mtd, from, &ops);
951 *retlen = ops.retlen;
956 if (unlikely(ret_code < 0))
958 if (mtd->ecc_strength == 0)
959 return 0; /* device lacks ecc */
960 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
962 EXPORT_SYMBOL_GPL(mtd_read);
964 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
968 if (to < 0 || to > mtd->size || len > mtd->size - to)
970 if ((!mtd->_write && !mtd->_write_oob) ||
971 !(mtd->flags & MTD_WRITEABLE))
977 struct mtd_oob_ops ops = {
983 ret = mtd->_write_oob(mtd, to, &ops);
984 *retlen = ops.retlen;
988 return mtd->_write(mtd, to, len, retlen, buf);
990 EXPORT_SYMBOL_GPL(mtd_write);
993 * In blackbox flight recorder like scenarios we want to make successful writes
994 * in interrupt context. panic_write() is only intended to be called when its
995 * known the kernel is about to panic and we need the write to succeed. Since
996 * the kernel is not going to be running for much longer, this function can
997 * break locks and delay to ensure the write succeeds (but not sleep).
999 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
1003 if (!mtd->_panic_write)
1005 if (to < 0 || to > mtd->size || len > mtd->size - to)
1007 if (!(mtd->flags & MTD_WRITEABLE))
1011 return mtd->_panic_write(mtd, to, len, retlen, buf);
1013 EXPORT_SYMBOL_GPL(mtd_panic_write);
1015 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
1016 struct mtd_oob_ops *ops)
1019 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1020 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1029 if (offs < 0 || offs + ops->len > mtd->size)
1035 if (ops->ooboffs >= mtd_oobavail(mtd, ops))
1038 maxooblen = ((mtd_div_by_ws(mtd->size, mtd) -
1039 mtd_div_by_ws(offs, mtd)) *
1040 mtd_oobavail(mtd, ops)) - ops->ooboffs;
1041 if (ops->ooblen > maxooblen)
1048 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
1051 ops->retlen = ops->oobretlen = 0;
1053 ret_code = mtd_check_oob_ops(mtd, from, ops);
1057 /* Check the validity of a potential fallback on mtd->_read */
1058 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
1062 ret_code = mtd->_read_oob(mtd, from, ops);
1064 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
1068 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1069 * similar to mtd->_read(), returning a non-negative integer
1070 * representing max bitflips. In other cases, mtd->_read_oob() may
1071 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1073 if (unlikely(ret_code < 0))
1075 if (mtd->ecc_strength == 0)
1076 return 0; /* device lacks ecc */
1077 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1079 EXPORT_SYMBOL_GPL(mtd_read_oob);
1081 int mtd_write_oob(struct mtd_info *mtd, loff_t to,
1082 struct mtd_oob_ops *ops)
1086 ops->retlen = ops->oobretlen = 0;
1088 if (!(mtd->flags & MTD_WRITEABLE))
1091 ret = mtd_check_oob_ops(mtd, to, ops);
1095 /* Check the validity of a potential fallback on mtd->_write */
1096 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
1099 if (mtd->_write_oob)
1100 return mtd->_write_oob(mtd, to, ops);
1102 return mtd->_write(mtd, to, ops->len, &ops->retlen,
1105 EXPORT_SYMBOL_GPL(mtd_write_oob);
1108 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1109 * @mtd: MTD device structure
1110 * @section: ECC section. Depending on the layout you may have all the ECC
1111 * bytes stored in a single contiguous section, or one section
1112 * per ECC chunk (and sometime several sections for a single ECC
1114 * @oobecc: OOB region struct filled with the appropriate ECC position
1117 * This function returns ECC section information in the OOB area. If you want
1118 * to get all the ECC bytes information, then you should call
1119 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1121 * Returns zero on success, a negative error code otherwise.
1123 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
1124 struct mtd_oob_region *oobecc)
1126 memset(oobecc, 0, sizeof(*oobecc));
1128 if (!mtd || section < 0)
1131 if (!mtd->ooblayout || !mtd->ooblayout->ecc)
1134 return mtd->ooblayout->ecc(mtd, section, oobecc);
1136 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
1139 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1141 * @mtd: MTD device structure
1142 * @section: Free section you are interested in. Depending on the layout
1143 * you may have all the free bytes stored in a single contiguous
1144 * section, or one section per ECC chunk plus an extra section
1145 * for the remaining bytes (or other funky layout).
1146 * @oobfree: OOB region struct filled with the appropriate free position
1149 * This function returns free bytes position in the OOB area. If you want
1150 * to get all the free bytes information, then you should call
1151 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1153 * Returns zero on success, a negative error code otherwise.
1155 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
1156 struct mtd_oob_region *oobfree)
1158 memset(oobfree, 0, sizeof(*oobfree));
1160 if (!mtd || section < 0)
1163 if (!mtd->ooblayout || !mtd->ooblayout->free)
1166 return mtd->ooblayout->free(mtd, section, oobfree);
1168 EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
1171 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1172 * @mtd: mtd info structure
1173 * @byte: the byte we are searching for
1174 * @sectionp: pointer where the section id will be stored
1175 * @oobregion: used to retrieve the ECC position
1176 * @iter: iterator function. Should be either mtd_ooblayout_free or
1177 * mtd_ooblayout_ecc depending on the region type you're searching for
1179 * This function returns the section id and oobregion information of a
1180 * specific byte. For example, say you want to know where the 4th ECC byte is
1181 * stored, you'll use:
1183 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1185 * Returns zero on success, a negative error code otherwise.
1187 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
1188 int *sectionp, struct mtd_oob_region *oobregion,
1189 int (*iter)(struct mtd_info *,
1191 struct mtd_oob_region *oobregion))
1193 int pos = 0, ret, section = 0;
1195 memset(oobregion, 0, sizeof(*oobregion));
1198 ret = iter(mtd, section, oobregion);
1202 if (pos + oobregion->length > byte)
1205 pos += oobregion->length;
1210 * Adjust region info to make it start at the beginning at the
1213 oobregion->offset += byte - pos;
1214 oobregion->length -= byte - pos;
1215 *sectionp = section;
1221 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1223 * @mtd: mtd info structure
1224 * @eccbyte: the byte we are searching for
1225 * @sectionp: pointer where the section id will be stored
1226 * @oobregion: OOB region information
1228 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1231 * Returns zero on success, a negative error code otherwise.
1233 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
1235 struct mtd_oob_region *oobregion)
1237 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
1240 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
1243 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1244 * @mtd: mtd info structure
1245 * @buf: destination buffer to store OOB bytes
1246 * @oobbuf: OOB buffer
1247 * @start: first byte to retrieve
1248 * @nbytes: number of bytes to retrieve
1249 * @iter: section iterator
1251 * Extract bytes attached to a specific category (ECC or free)
1252 * from the OOB buffer and copy them into buf.
1254 * Returns zero on success, a negative error code otherwise.
1256 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
1257 const u8 *oobbuf, int start, int nbytes,
1258 int (*iter)(struct mtd_info *,
1260 struct mtd_oob_region *oobregion))
1262 struct mtd_oob_region oobregion;
1265 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1271 cnt = min_t(int, nbytes, oobregion.length);
1272 memcpy(buf, oobbuf + oobregion.offset, cnt);
1279 ret = iter(mtd, ++section, &oobregion);
1286 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1287 * @mtd: mtd info structure
1288 * @buf: source buffer to get OOB bytes from
1289 * @oobbuf: OOB buffer
1290 * @start: first OOB byte to set
1291 * @nbytes: number of OOB bytes to set
1292 * @iter: section iterator
1294 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1295 * is selected by passing the appropriate iterator.
1297 * Returns zero on success, a negative error code otherwise.
1299 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
1300 u8 *oobbuf, int start, int nbytes,
1301 int (*iter)(struct mtd_info *,
1303 struct mtd_oob_region *oobregion))
1305 struct mtd_oob_region oobregion;
1308 ret = mtd_ooblayout_find_region(mtd, start, §ion,
1314 cnt = min_t(int, nbytes, oobregion.length);
1315 memcpy(oobbuf + oobregion.offset, buf, cnt);
1322 ret = iter(mtd, ++section, &oobregion);
1329 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1330 * @mtd: mtd info structure
1331 * @iter: category iterator
1333 * Count the number of bytes in a given category.
1335 * Returns a positive value on success, a negative error code otherwise.
1337 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
1338 int (*iter)(struct mtd_info *,
1340 struct mtd_oob_region *oobregion))
1342 struct mtd_oob_region oobregion;
1343 int section = 0, ret, nbytes = 0;
1346 ret = iter(mtd, section++, &oobregion);
1353 nbytes += oobregion.length;
1360 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1361 * @mtd: mtd info structure
1362 * @eccbuf: destination buffer to store ECC bytes
1363 * @oobbuf: OOB buffer
1364 * @start: first ECC byte to retrieve
1365 * @nbytes: number of ECC bytes to retrieve
1367 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1369 * Returns zero on success, a negative error code otherwise.
1371 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
1372 const u8 *oobbuf, int start, int nbytes)
1374 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1377 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
1380 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1381 * @mtd: mtd info structure
1382 * @eccbuf: source buffer to get ECC bytes from
1383 * @oobbuf: OOB buffer
1384 * @start: first ECC byte to set
1385 * @nbytes: number of ECC bytes to set
1387 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1389 * Returns zero on success, a negative error code otherwise.
1391 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
1392 u8 *oobbuf, int start, int nbytes)
1394 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
1397 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
1400 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1401 * @mtd: mtd info structure
1402 * @databuf: destination buffer to store ECC bytes
1403 * @oobbuf: OOB buffer
1404 * @start: first ECC byte to retrieve
1405 * @nbytes: number of ECC bytes to retrieve
1407 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1409 * Returns zero on success, a negative error code otherwise.
1411 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
1412 const u8 *oobbuf, int start, int nbytes)
1414 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
1415 mtd_ooblayout_free);
1417 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
1420 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
1421 * @mtd: mtd info structure
1422 * @eccbuf: source buffer to get data bytes from
1423 * @oobbuf: OOB buffer
1424 * @start: first ECC byte to set
1425 * @nbytes: number of ECC bytes to set
1427 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1429 * Returns zero on success, a negative error code otherwise.
1431 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
1432 u8 *oobbuf, int start, int nbytes)
1434 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
1435 mtd_ooblayout_free);
1437 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
1440 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1441 * @mtd: mtd info structure
1443 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1445 * Returns zero on success, a negative error code otherwise.
1447 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
1449 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
1451 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
1454 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
1455 * @mtd: mtd info structure
1457 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1459 * Returns zero on success, a negative error code otherwise.
1461 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
1463 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
1465 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
1468 * Method to access the protection register area, present in some flash
1469 * devices. The user data is one time programmable but the factory data is read
1472 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1473 struct otp_info *buf)
1475 if (!mtd->_get_fact_prot_info)
1479 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1481 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1483 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1484 size_t *retlen, u_char *buf)
1487 if (!mtd->_read_fact_prot_reg)
1491 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1493 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1495 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1496 struct otp_info *buf)
1498 if (!mtd->_get_user_prot_info)
1502 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1504 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1506 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1507 size_t *retlen, u_char *buf)
1510 if (!mtd->_read_user_prot_reg)
1514 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1516 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1518 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1519 size_t *retlen, u_char *buf)
1524 if (!mtd->_write_user_prot_reg)
1528 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1533 * If no data could be written at all, we are out of memory and
1534 * must return -ENOSPC.
1536 return (*retlen) ? 0 : -ENOSPC;
1538 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1540 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1542 if (!mtd->_lock_user_prot_reg)
1546 return mtd->_lock_user_prot_reg(mtd, from, len);
1548 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1550 /* Chip-supported device locking */
1551 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1555 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1559 return mtd->_lock(mtd, ofs, len);
1561 EXPORT_SYMBOL_GPL(mtd_lock);
1563 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1567 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1571 return mtd->_unlock(mtd, ofs, len);
1573 EXPORT_SYMBOL_GPL(mtd_unlock);
1575 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1577 if (!mtd->_is_locked)
1579 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1583 return mtd->_is_locked(mtd, ofs, len);
1585 EXPORT_SYMBOL_GPL(mtd_is_locked);
1587 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
1589 if (ofs < 0 || ofs > mtd->size)
1591 if (!mtd->_block_isreserved)
1593 return mtd->_block_isreserved(mtd, ofs);
1595 EXPORT_SYMBOL_GPL(mtd_block_isreserved);
1597 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1599 if (ofs < 0 || ofs > mtd->size)
1601 if (!mtd->_block_isbad)
1603 return mtd->_block_isbad(mtd, ofs);
1605 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1607 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1609 if (!mtd->_block_markbad)
1611 if (ofs < 0 || ofs > mtd->size)
1613 if (!(mtd->flags & MTD_WRITEABLE))
1615 return mtd->_block_markbad(mtd, ofs);
1617 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1621 * default_mtd_writev - the default writev method
1622 * @mtd: mtd device description object pointer
1623 * @vecs: the vectors to write
1624 * @count: count of vectors in @vecs
1625 * @to: the MTD device offset to write to
1626 * @retlen: on exit contains the count of bytes written to the MTD device.
1628 * This function returns zero in case of success and a negative error code in
1631 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1632 unsigned long count, loff_t to, size_t *retlen)
1635 size_t totlen = 0, thislen;
1638 for (i = 0; i < count; i++) {
1639 if (!vecs[i].iov_len)
1641 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1644 if (ret || thislen != vecs[i].iov_len)
1646 to += vecs[i].iov_len;
1653 * mtd_writev - the vector-based MTD write method
1654 * @mtd: mtd device description object pointer
1655 * @vecs: the vectors to write
1656 * @count: count of vectors in @vecs
1657 * @to: the MTD device offset to write to
1658 * @retlen: on exit contains the count of bytes written to the MTD device.
1660 * This function returns zero in case of success and a negative error code in
1663 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1664 unsigned long count, loff_t to, size_t *retlen)
1667 if (!(mtd->flags & MTD_WRITEABLE))
1670 return default_mtd_writev(mtd, vecs, count, to, retlen);
1671 return mtd->_writev(mtd, vecs, count, to, retlen);
1673 EXPORT_SYMBOL_GPL(mtd_writev);
1676 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1677 * @mtd: mtd device description object pointer
1678 * @size: a pointer to the ideal or maximum size of the allocation, points
1679 * to the actual allocation size on success.
1681 * This routine attempts to allocate a contiguous kernel buffer up to
1682 * the specified size, backing off the size of the request exponentially
1683 * until the request succeeds or until the allocation size falls below
1684 * the system page size. This attempts to make sure it does not adversely
1685 * impact system performance, so when allocating more than one page, we
1686 * ask the memory allocator to avoid re-trying, swapping, writing back
1687 * or performing I/O.
1689 * Note, this function also makes sure that the allocated buffer is aligned to
1690 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1692 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1693 * to handle smaller (i.e. degraded) buffer allocations under low- or
1694 * fragmented-memory situations where such reduced allocations, from a
1695 * requested ideal, are allowed.
1697 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1699 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1701 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1702 __GFP_NORETRY | __GFP_NO_KSWAPD;
1703 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1706 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1708 while (*size > min_alloc) {
1709 kbuf = kmalloc(*size, flags);
1714 *size = ALIGN(*size, mtd->writesize);
1718 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1719 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1721 return kmalloc(*size, GFP_KERNEL);
1723 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1726 #ifdef CONFIG_PROC_FS
1728 /*====================================================================*/
1729 /* Support for /proc/mtd */
1731 static int mtd_proc_show(struct seq_file *m, void *v)
1733 struct mtd_info *mtd;
1735 seq_puts(m, "dev: size erasesize name\n");
1736 mutex_lock(&mtd_table_mutex);
1737 mtd_for_each_device(mtd) {
1738 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1739 mtd->index, (unsigned long long)mtd->size,
1740 mtd->erasesize, mtd->name);
1742 mutex_unlock(&mtd_table_mutex);
1746 static int mtd_proc_open(struct inode *inode, struct file *file)
1748 return single_open(file, mtd_proc_show, NULL);
1751 static const struct file_operations mtd_proc_ops = {
1752 .open = mtd_proc_open,
1754 .llseek = seq_lseek,
1755 .release = single_release,
1757 #endif /* CONFIG_PROC_FS */
1759 /*====================================================================*/
1763 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1767 ret = bdi_init(bdi);
1769 ret = bdi_register(bdi, NULL, "%s", name);
1777 static struct proc_dir_entry *proc_mtd;
1779 static int __init init_mtd(void)
1783 ret = class_register(&mtd_class);
1787 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1791 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1795 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1799 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1801 ret = init_mtdchar();
1809 remove_proc_entry("mtd", NULL);
1811 bdi_destroy(&mtd_bdi_ro_mappable);
1813 bdi_destroy(&mtd_bdi_unmappable);
1815 class_unregister(&mtd_class);
1817 pr_err("Error registering mtd class or bdi: %d\n", ret);
1821 static void __exit cleanup_mtd(void)
1825 remove_proc_entry("mtd", NULL);
1826 class_unregister(&mtd_class);
1827 bdi_destroy(&mtd_bdi_unmappable);
1828 bdi_destroy(&mtd_bdi_ro_mappable);
1829 bdi_destroy(&mtd_bdi_rw_mappable);
1832 module_init(init_mtd);
1833 module_exit(cleanup_mtd);
1836 MODULE_LICENSE("GPL");
1837 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1838 MODULE_DESCRIPTION("Core MTD registration and access routines");