2 * Core registration and callback routines for MTD
5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2006 Red Hat UK Limited
8 * SPDX-License-Identifier: GPL-2.0+
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/compat.h>
32 #include <linux/err.h>
33 #include <ubi_uboot.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.h>
43 * backing device capabilities for non-mappable devices (such as NAND flash)
44 * - permits private mappings, copies are taken of the data
46 static struct backing_dev_info mtd_bdi_unmappable = {
47 .capabilities = BDI_CAP_MAP_COPY,
51 * backing device capabilities for R/O mappable devices (such as ROM)
52 * - permits private mappings, copies are taken of the data
53 * - permits non-writable shared mappings
55 static struct backing_dev_info mtd_bdi_ro_mappable = {
56 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
57 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
61 * backing device capabilities for writable mappable devices (such as RAM)
62 * - permits private mappings, copies are taken of the data
63 * - permits non-writable shared mappings
65 static struct backing_dev_info mtd_bdi_rw_mappable = {
66 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
67 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
71 static int mtd_cls_suspend(struct device *dev, pm_message_t state);
72 static int mtd_cls_resume(struct device *dev);
74 static struct class mtd_class = {
77 .suspend = mtd_cls_suspend,
78 .resume = mtd_cls_resume,
81 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
91 struct idr_layer id[MAX_IDR_ID];
94 #define DEFINE_IDR(name) struct idr name;
96 void idr_remove(struct idr *idp, int id)
103 void *idr_find(struct idr *idp, int id)
105 if (idp->id[id].used)
106 return idp->id[id].ptr;
111 void *idr_get_next(struct idr *idp, int *next)
116 ret = idr_find(idp, id);
119 if (!idp->id[id].used)
129 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
131 struct idr_layer *idl;
134 while (i < MAX_IDR_ID) {
136 if (idl->used == 0) {
147 static DEFINE_IDR(mtd_idr);
149 /* These are exported solely for the purpose of mtd_blkdevs.c. You
150 should not use them for _anything_ else */
151 DEFINE_MUTEX(mtd_table_mutex);
152 EXPORT_SYMBOL_GPL(mtd_table_mutex);
154 struct mtd_info *__mtd_next_device(int i)
156 return idr_get_next(&mtd_idr, &i);
158 EXPORT_SYMBOL_GPL(__mtd_next_device);
161 static LIST_HEAD(mtd_notifiers);
164 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
166 /* REVISIT once MTD uses the driver model better, whoever allocates
167 * the mtd_info will probably want to use the release() hook...
169 static void mtd_release(struct device *dev)
171 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
172 dev_t index = MTD_DEVT(mtd->index);
174 /* remove /dev/mtdXro node if needed */
176 device_destroy(&mtd_class, index + 1);
179 static int mtd_cls_suspend(struct device *dev, pm_message_t state)
181 struct mtd_info *mtd = dev_get_drvdata(dev);
183 return mtd ? mtd_suspend(mtd) : 0;
186 static int mtd_cls_resume(struct device *dev)
188 struct mtd_info *mtd = dev_get_drvdata(dev);
195 static ssize_t mtd_type_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
198 struct mtd_info *mtd = dev_get_drvdata(dev);
223 case MTD_MLCNANDFLASH:
230 return snprintf(buf, PAGE_SIZE, "%s\n", type);
232 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
234 static ssize_t mtd_flags_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct mtd_info *mtd = dev_get_drvdata(dev);
239 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
242 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
244 static ssize_t mtd_size_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct mtd_info *mtd = dev_get_drvdata(dev);
249 return snprintf(buf, PAGE_SIZE, "%llu\n",
250 (unsigned long long)mtd->size);
253 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
255 static ssize_t mtd_erasesize_show(struct device *dev,
256 struct device_attribute *attr, char *buf)
258 struct mtd_info *mtd = dev_get_drvdata(dev);
260 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
263 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
265 static ssize_t mtd_writesize_show(struct device *dev,
266 struct device_attribute *attr, char *buf)
268 struct mtd_info *mtd = dev_get_drvdata(dev);
270 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
273 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
275 static ssize_t mtd_subpagesize_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
278 struct mtd_info *mtd = dev_get_drvdata(dev);
279 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
281 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
284 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
286 static ssize_t mtd_oobsize_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct mtd_info *mtd = dev_get_drvdata(dev);
291 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
294 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
296 static ssize_t mtd_numeraseregions_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct mtd_info *mtd = dev_get_drvdata(dev);
301 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
304 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
307 static ssize_t mtd_name_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct mtd_info *mtd = dev_get_drvdata(dev);
312 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
315 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
317 static ssize_t mtd_ecc_strength_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 struct mtd_info *mtd = dev_get_drvdata(dev);
322 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
324 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
326 static ssize_t mtd_bitflip_threshold_show(struct device *dev,
327 struct device_attribute *attr,
330 struct mtd_info *mtd = dev_get_drvdata(dev);
332 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
335 static ssize_t mtd_bitflip_threshold_store(struct device *dev,
336 struct device_attribute *attr,
337 const char *buf, size_t count)
339 struct mtd_info *mtd = dev_get_drvdata(dev);
340 unsigned int bitflip_threshold;
343 retval = kstrtouint(buf, 0, &bitflip_threshold);
347 mtd->bitflip_threshold = bitflip_threshold;
350 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
351 mtd_bitflip_threshold_show,
352 mtd_bitflip_threshold_store);
354 static ssize_t mtd_ecc_step_size_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
357 struct mtd_info *mtd = dev_get_drvdata(dev);
359 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
362 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
364 static struct attribute *mtd_attrs[] = {
366 &dev_attr_flags.attr,
368 &dev_attr_erasesize.attr,
369 &dev_attr_writesize.attr,
370 &dev_attr_subpagesize.attr,
371 &dev_attr_oobsize.attr,
372 &dev_attr_numeraseregions.attr,
374 &dev_attr_ecc_strength.attr,
375 &dev_attr_ecc_step_size.attr,
376 &dev_attr_bitflip_threshold.attr,
379 ATTRIBUTE_GROUPS(mtd);
381 static struct device_type mtd_devtype = {
383 .groups = mtd_groups,
384 .release = mtd_release,
389 * add_mtd_device - register an MTD device
390 * @mtd: pointer to new MTD device info structure
392 * Add a device to the list of MTD devices present in the system, and
393 * notify each currently active MTD 'user' of its arrival. Returns
394 * zero on success or 1 on failure, which currently will only happen
395 * if there is insufficient memory or a sysfs error.
398 int add_mtd_device(struct mtd_info *mtd)
401 struct mtd_notifier *not;
406 if (!mtd->backing_dev_info) {
409 mtd->backing_dev_info = &mtd_bdi_rw_mappable;
412 mtd->backing_dev_info = &mtd_bdi_ro_mappable;
415 mtd->backing_dev_info = &mtd_bdi_unmappable;
421 BUG_ON(mtd->writesize == 0);
422 mutex_lock(&mtd_table_mutex);
424 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
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).
942 ret_code = mtd->_read(mtd, from, len, retlen, buf);
943 if (unlikely(ret_code < 0))
945 if (mtd->ecc_strength == 0)
946 return 0; /* device lacks ecc */
947 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
949 EXPORT_SYMBOL_GPL(mtd_read);
951 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
955 if (to < 0 || to > mtd->size || len > mtd->size - to)
957 if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
961 return mtd->_write(mtd, to, len, retlen, buf);
963 EXPORT_SYMBOL_GPL(mtd_write);
966 * In blackbox flight recorder like scenarios we want to make successful writes
967 * in interrupt context. panic_write() is only intended to be called when its
968 * known the kernel is about to panic and we need the write to succeed. Since
969 * the kernel is not going to be running for much longer, this function can
970 * break locks and delay to ensure the write succeeds (but not sleep).
972 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
976 if (!mtd->_panic_write)
978 if (to < 0 || to > mtd->size || len > mtd->size - to)
980 if (!(mtd->flags & MTD_WRITEABLE))
984 return mtd->_panic_write(mtd, to, len, retlen, buf);
986 EXPORT_SYMBOL_GPL(mtd_panic_write);
988 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
991 ops->retlen = ops->oobretlen = 0;
995 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
996 * similar to mtd->_read(), returning a non-negative integer
997 * representing max bitflips. In other cases, mtd->_read_oob() may
998 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1000 ret_code = mtd->_read_oob(mtd, from, ops);
1001 if (unlikely(ret_code < 0))
1003 if (mtd->ecc_strength == 0)
1004 return 0; /* device lacks ecc */
1005 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
1007 EXPORT_SYMBOL_GPL(mtd_read_oob);
1010 * Method to access the protection register area, present in some flash
1011 * devices. The user data is one time programmable but the factory data is read
1014 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1015 struct otp_info *buf)
1017 if (!mtd->_get_fact_prot_info)
1021 return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
1023 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
1025 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1026 size_t *retlen, u_char *buf)
1029 if (!mtd->_read_fact_prot_reg)
1033 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
1035 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
1037 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
1038 struct otp_info *buf)
1040 if (!mtd->_get_user_prot_info)
1044 return mtd->_get_user_prot_info(mtd, len, retlen, buf);
1046 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
1048 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
1049 size_t *retlen, u_char *buf)
1052 if (!mtd->_read_user_prot_reg)
1056 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
1058 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
1060 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
1061 size_t *retlen, u_char *buf)
1066 if (!mtd->_write_user_prot_reg)
1070 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
1075 * If no data could be written at all, we are out of memory and
1076 * must return -ENOSPC.
1078 return (*retlen) ? 0 : -ENOSPC;
1080 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
1082 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
1084 if (!mtd->_lock_user_prot_reg)
1088 return mtd->_lock_user_prot_reg(mtd, from, len);
1090 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
1092 /* Chip-supported device locking */
1093 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1097 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1101 return mtd->_lock(mtd, ofs, len);
1103 EXPORT_SYMBOL_GPL(mtd_lock);
1105 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1109 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1113 return mtd->_unlock(mtd, ofs, len);
1115 EXPORT_SYMBOL_GPL(mtd_unlock);
1117 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1119 if (!mtd->_is_locked)
1121 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
1125 return mtd->_is_locked(mtd, ofs, len);
1127 EXPORT_SYMBOL_GPL(mtd_is_locked);
1129 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
1131 if (!mtd->_block_isbad)
1133 if (ofs < 0 || ofs > mtd->size)
1135 return mtd->_block_isbad(mtd, ofs);
1137 EXPORT_SYMBOL_GPL(mtd_block_isbad);
1139 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
1141 if (!mtd->_block_markbad)
1143 if (ofs < 0 || ofs > mtd->size)
1145 if (!(mtd->flags & MTD_WRITEABLE))
1147 return mtd->_block_markbad(mtd, ofs);
1149 EXPORT_SYMBOL_GPL(mtd_block_markbad);
1153 * default_mtd_writev - the default writev method
1154 * @mtd: mtd device description object pointer
1155 * @vecs: the vectors to write
1156 * @count: count of vectors in @vecs
1157 * @to: the MTD device offset to write to
1158 * @retlen: on exit contains the count of bytes written to the MTD device.
1160 * This function returns zero in case of success and a negative error code in
1163 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1164 unsigned long count, loff_t to, size_t *retlen)
1167 size_t totlen = 0, thislen;
1170 for (i = 0; i < count; i++) {
1171 if (!vecs[i].iov_len)
1173 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
1176 if (ret || thislen != vecs[i].iov_len)
1178 to += vecs[i].iov_len;
1185 * mtd_writev - the vector-based MTD write method
1186 * @mtd: mtd device description object pointer
1187 * @vecs: the vectors to write
1188 * @count: count of vectors in @vecs
1189 * @to: the MTD device offset to write to
1190 * @retlen: on exit contains the count of bytes written to the MTD device.
1192 * This function returns zero in case of success and a negative error code in
1195 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
1196 unsigned long count, loff_t to, size_t *retlen)
1199 if (!(mtd->flags & MTD_WRITEABLE))
1202 return default_mtd_writev(mtd, vecs, count, to, retlen);
1203 return mtd->_writev(mtd, vecs, count, to, retlen);
1205 EXPORT_SYMBOL_GPL(mtd_writev);
1208 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1209 * @mtd: mtd device description object pointer
1210 * @size: a pointer to the ideal or maximum size of the allocation, points
1211 * to the actual allocation size on success.
1213 * This routine attempts to allocate a contiguous kernel buffer up to
1214 * the specified size, backing off the size of the request exponentially
1215 * until the request succeeds or until the allocation size falls below
1216 * the system page size. This attempts to make sure it does not adversely
1217 * impact system performance, so when allocating more than one page, we
1218 * ask the memory allocator to avoid re-trying, swapping, writing back
1219 * or performing I/O.
1221 * Note, this function also makes sure that the allocated buffer is aligned to
1222 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1224 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1225 * to handle smaller (i.e. degraded) buffer allocations under low- or
1226 * fragmented-memory situations where such reduced allocations, from a
1227 * requested ideal, are allowed.
1229 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1231 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
1233 gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
1234 __GFP_NORETRY | __GFP_NO_KSWAPD;
1235 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
1238 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
1240 while (*size > min_alloc) {
1241 kbuf = kmalloc(*size, flags);
1246 *size = ALIGN(*size, mtd->writesize);
1250 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1251 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1253 return kmalloc(*size, GFP_KERNEL);
1255 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
1258 #ifdef CONFIG_PROC_FS
1260 /*====================================================================*/
1261 /* Support for /proc/mtd */
1263 static int mtd_proc_show(struct seq_file *m, void *v)
1265 struct mtd_info *mtd;
1267 seq_puts(m, "dev: size erasesize name\n");
1268 mutex_lock(&mtd_table_mutex);
1269 mtd_for_each_device(mtd) {
1270 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1271 mtd->index, (unsigned long long)mtd->size,
1272 mtd->erasesize, mtd->name);
1274 mutex_unlock(&mtd_table_mutex);
1278 static int mtd_proc_open(struct inode *inode, struct file *file)
1280 return single_open(file, mtd_proc_show, NULL);
1283 static const struct file_operations mtd_proc_ops = {
1284 .open = mtd_proc_open,
1286 .llseek = seq_lseek,
1287 .release = single_release,
1289 #endif /* CONFIG_PROC_FS */
1291 /*====================================================================*/
1295 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
1299 ret = bdi_init(bdi);
1301 ret = bdi_register(bdi, NULL, "%s", name);
1309 static struct proc_dir_entry *proc_mtd;
1311 static int __init init_mtd(void)
1315 ret = class_register(&mtd_class);
1319 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
1323 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
1327 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
1331 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
1333 ret = init_mtdchar();
1341 remove_proc_entry("mtd", NULL);
1343 bdi_destroy(&mtd_bdi_ro_mappable);
1345 bdi_destroy(&mtd_bdi_unmappable);
1347 class_unregister(&mtd_class);
1349 pr_err("Error registering mtd class or bdi: %d\n", ret);
1353 static void __exit cleanup_mtd(void)
1357 remove_proc_entry("mtd", NULL);
1358 class_unregister(&mtd_class);
1359 bdi_destroy(&mtd_bdi_unmappable);
1360 bdi_destroy(&mtd_bdi_ro_mappable);
1361 bdi_destroy(&mtd_bdi_rw_mappable);
1364 module_init(init_mtd);
1365 module_exit(cleanup_mtd);
1368 MODULE_LICENSE("GPL");
1369 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1370 MODULE_DESCRIPTION("Core MTD registration and access routines");