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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/ptrace.h>
27 #include <linux/seq_file.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/major.h>
32 #include <linux/err.h>
33 #include <linux/ioctl.h>
34 #include <linux/init.h>
36 #include <linux/proc_fs.h>
37 #include <linux/idr.h>
38 #include <linux/backing-dev.h>
39 #include <linux/gfp.h>
40 #include <linux/slab.h>
41 #include <linux/reboot.h>
42 #include <linux/leds.h>
43 #include <linux/debugfs.h>
44 #include <linux/nvmem-provider.h>
46 #include <linux/mtd/mtd.h>
47 #include <linux/mtd/partitions.h>
51 struct backing_dev_info
*mtd_bdi
;
53 #ifdef CONFIG_PM_SLEEP
55 static int mtd_cls_suspend(struct device
*dev
)
57 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
59 return mtd
? mtd_suspend(mtd
) : 0;
62 static int mtd_cls_resume(struct device
*dev
)
64 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
71 static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops
, mtd_cls_suspend
, mtd_cls_resume
);
72 #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops)
74 #define MTD_CLS_PM_OPS NULL
77 static struct class mtd_class
= {
83 static DEFINE_IDR(mtd_idr
);
85 /* These are exported solely for the purpose of mtd_blkdevs.c. You
86 should not use them for _anything_ else */
87 DEFINE_MUTEX(mtd_table_mutex
);
88 EXPORT_SYMBOL_GPL(mtd_table_mutex
);
90 struct mtd_info
*__mtd_next_device(int i
)
92 return idr_get_next(&mtd_idr
, &i
);
94 EXPORT_SYMBOL_GPL(__mtd_next_device
);
96 static LIST_HEAD(mtd_notifiers
);
99 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
101 /* REVISIT once MTD uses the driver model better, whoever allocates
102 * the mtd_info will probably want to use the release() hook...
104 static void mtd_release(struct device
*dev
)
106 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
107 dev_t index
= MTD_DEVT(mtd
->index
);
109 /* remove /dev/mtdXro node */
110 device_destroy(&mtd_class
, index
+ 1);
113 static ssize_t
mtd_type_show(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
141 case MTD_MLCNANDFLASH
:
148 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
150 static DEVICE_ATTR(type
, S_IRUGO
, mtd_type_show
, NULL
);
152 static ssize_t
mtd_flags_show(struct device
*dev
,
153 struct device_attribute
*attr
, char *buf
)
155 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
157 return snprintf(buf
, PAGE_SIZE
, "0x%lx\n", (unsigned long)mtd
->flags
);
160 static DEVICE_ATTR(flags
, S_IRUGO
, mtd_flags_show
, NULL
);
162 static ssize_t
mtd_size_show(struct device
*dev
,
163 struct device_attribute
*attr
, char *buf
)
165 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
167 return snprintf(buf
, PAGE_SIZE
, "%llu\n",
168 (unsigned long long)mtd
->size
);
171 static DEVICE_ATTR(size
, S_IRUGO
, mtd_size_show
, NULL
);
173 static ssize_t
mtd_erasesize_show(struct device
*dev
,
174 struct device_attribute
*attr
, char *buf
)
176 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
178 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->erasesize
);
181 static DEVICE_ATTR(erasesize
, S_IRUGO
, mtd_erasesize_show
, NULL
);
183 static ssize_t
mtd_writesize_show(struct device
*dev
,
184 struct device_attribute
*attr
, char *buf
)
186 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
188 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->writesize
);
191 static DEVICE_ATTR(writesize
, S_IRUGO
, mtd_writesize_show
, NULL
);
193 static ssize_t
mtd_subpagesize_show(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
196 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
197 unsigned int subpagesize
= mtd
->writesize
>> mtd
->subpage_sft
;
199 return snprintf(buf
, PAGE_SIZE
, "%u\n", subpagesize
);
202 static DEVICE_ATTR(subpagesize
, S_IRUGO
, mtd_subpagesize_show
, NULL
);
204 static ssize_t
mtd_oobsize_show(struct device
*dev
,
205 struct device_attribute
*attr
, char *buf
)
207 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
209 return snprintf(buf
, PAGE_SIZE
, "%lu\n", (unsigned long)mtd
->oobsize
);
212 static DEVICE_ATTR(oobsize
, S_IRUGO
, mtd_oobsize_show
, NULL
);
214 static ssize_t
mtd_oobavail_show(struct device
*dev
,
215 struct device_attribute
*attr
, char *buf
)
217 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
219 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->oobavail
);
221 static DEVICE_ATTR(oobavail
, S_IRUGO
, mtd_oobavail_show
, NULL
);
223 static ssize_t
mtd_numeraseregions_show(struct device
*dev
,
224 struct device_attribute
*attr
, char *buf
)
226 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
228 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->numeraseregions
);
231 static DEVICE_ATTR(numeraseregions
, S_IRUGO
, mtd_numeraseregions_show
,
234 static ssize_t
mtd_name_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
, "%s\n", mtd
->name
);
242 static DEVICE_ATTR(name
, S_IRUGO
, mtd_name_show
, NULL
);
244 static ssize_t
mtd_ecc_strength_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
, "%u\n", mtd
->ecc_strength
);
251 static DEVICE_ATTR(ecc_strength
, S_IRUGO
, mtd_ecc_strength_show
, NULL
);
253 static ssize_t
mtd_bitflip_threshold_show(struct device
*dev
,
254 struct device_attribute
*attr
,
257 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
259 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->bitflip_threshold
);
262 static ssize_t
mtd_bitflip_threshold_store(struct device
*dev
,
263 struct device_attribute
*attr
,
264 const char *buf
, size_t count
)
266 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
267 unsigned int bitflip_threshold
;
270 retval
= kstrtouint(buf
, 0, &bitflip_threshold
);
274 mtd
->bitflip_threshold
= bitflip_threshold
;
277 static DEVICE_ATTR(bitflip_threshold
, S_IRUGO
| S_IWUSR
,
278 mtd_bitflip_threshold_show
,
279 mtd_bitflip_threshold_store
);
281 static ssize_t
mtd_ecc_step_size_show(struct device
*dev
,
282 struct device_attribute
*attr
, char *buf
)
284 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
286 return snprintf(buf
, PAGE_SIZE
, "%u\n", mtd
->ecc_step_size
);
289 static DEVICE_ATTR(ecc_step_size
, S_IRUGO
, mtd_ecc_step_size_show
, NULL
);
291 static ssize_t
mtd_ecc_stats_corrected_show(struct device
*dev
,
292 struct device_attribute
*attr
, char *buf
)
294 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
295 struct mtd_ecc_stats
*ecc_stats
= &mtd
->ecc_stats
;
297 return snprintf(buf
, PAGE_SIZE
, "%u\n", ecc_stats
->corrected
);
299 static DEVICE_ATTR(corrected_bits
, S_IRUGO
,
300 mtd_ecc_stats_corrected_show
, NULL
);
302 static ssize_t
mtd_ecc_stats_errors_show(struct device
*dev
,
303 struct device_attribute
*attr
, char *buf
)
305 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
306 struct mtd_ecc_stats
*ecc_stats
= &mtd
->ecc_stats
;
308 return snprintf(buf
, PAGE_SIZE
, "%u\n", ecc_stats
->failed
);
310 static DEVICE_ATTR(ecc_failures
, S_IRUGO
, mtd_ecc_stats_errors_show
, NULL
);
312 static ssize_t
mtd_badblocks_show(struct device
*dev
,
313 struct device_attribute
*attr
, char *buf
)
315 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
316 struct mtd_ecc_stats
*ecc_stats
= &mtd
->ecc_stats
;
318 return snprintf(buf
, PAGE_SIZE
, "%u\n", ecc_stats
->badblocks
);
320 static DEVICE_ATTR(bad_blocks
, S_IRUGO
, mtd_badblocks_show
, NULL
);
322 static ssize_t
mtd_bbtblocks_show(struct device
*dev
,
323 struct device_attribute
*attr
, char *buf
)
325 struct mtd_info
*mtd
= dev_get_drvdata(dev
);
326 struct mtd_ecc_stats
*ecc_stats
= &mtd
->ecc_stats
;
328 return snprintf(buf
, PAGE_SIZE
, "%u\n", ecc_stats
->bbtblocks
);
330 static DEVICE_ATTR(bbt_blocks
, S_IRUGO
, mtd_bbtblocks_show
, NULL
);
332 static struct attribute
*mtd_attrs
[] = {
334 &dev_attr_flags
.attr
,
336 &dev_attr_erasesize
.attr
,
337 &dev_attr_writesize
.attr
,
338 &dev_attr_subpagesize
.attr
,
339 &dev_attr_oobsize
.attr
,
340 &dev_attr_oobavail
.attr
,
341 &dev_attr_numeraseregions
.attr
,
343 &dev_attr_ecc_strength
.attr
,
344 &dev_attr_ecc_step_size
.attr
,
345 &dev_attr_corrected_bits
.attr
,
346 &dev_attr_ecc_failures
.attr
,
347 &dev_attr_bad_blocks
.attr
,
348 &dev_attr_bbt_blocks
.attr
,
349 &dev_attr_bitflip_threshold
.attr
,
352 ATTRIBUTE_GROUPS(mtd
);
354 static const struct device_type mtd_devtype
= {
356 .groups
= mtd_groups
,
357 .release
= mtd_release
,
361 unsigned mtd_mmap_capabilities(struct mtd_info
*mtd
)
365 return NOMMU_MAP_COPY
| NOMMU_MAP_DIRECT
| NOMMU_MAP_EXEC
|
366 NOMMU_MAP_READ
| NOMMU_MAP_WRITE
;
368 return NOMMU_MAP_COPY
| NOMMU_MAP_DIRECT
| NOMMU_MAP_EXEC
|
371 return NOMMU_MAP_COPY
;
374 EXPORT_SYMBOL_GPL(mtd_mmap_capabilities
);
377 static int mtd_reboot_notifier(struct notifier_block
*n
, unsigned long state
,
380 struct mtd_info
*mtd
;
382 mtd
= container_of(n
, struct mtd_info
, reboot_notifier
);
389 * mtd_wunit_to_pairing_info - get pairing information of a wunit
390 * @mtd: pointer to new MTD device info structure
391 * @wunit: write unit we are interested in
392 * @info: returned pairing information
394 * Retrieve pairing information associated to the wunit.
395 * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
396 * paired together, and where programming a page may influence the page it is
398 * The notion of page is replaced by the term wunit (write-unit) to stay
399 * consistent with the ->writesize field.
401 * The @wunit argument can be extracted from an absolute offset using
402 * mtd_offset_to_wunit(). @info is filled with the pairing information attached
405 * From the pairing info the MTD user can find all the wunits paired with
406 * @wunit using the following loop:
408 * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
410 * mtd_pairing_info_to_wunit(mtd, &info);
414 int mtd_wunit_to_pairing_info(struct mtd_info
*mtd
, int wunit
,
415 struct mtd_pairing_info
*info
)
417 int npairs
= mtd_wunit_per_eb(mtd
) / mtd_pairing_groups(mtd
);
419 if (wunit
< 0 || wunit
>= npairs
)
422 if (mtd
->pairing
&& mtd
->pairing
->get_info
)
423 return mtd
->pairing
->get_info(mtd
, wunit
, info
);
430 EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info
);
433 * mtd_pairing_info_to_wunit - get wunit from pairing information
434 * @mtd: pointer to new MTD device info structure
435 * @info: pairing information struct
437 * Returns a positive number representing the wunit associated to the info
438 * struct, or a negative error code.
440 * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
441 * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
444 * It can also be used to only program the first page of each pair (i.e.
445 * page attached to group 0), which allows one to use an MLC NAND in
446 * software-emulated SLC mode:
449 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
450 * for (info.pair = 0; info.pair < npairs; info.pair++) {
451 * wunit = mtd_pairing_info_to_wunit(mtd, &info);
452 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
453 * mtd->writesize, &retlen, buf + (i * mtd->writesize));
456 int mtd_pairing_info_to_wunit(struct mtd_info
*mtd
,
457 const struct mtd_pairing_info
*info
)
459 int ngroups
= mtd_pairing_groups(mtd
);
460 int npairs
= mtd_wunit_per_eb(mtd
) / ngroups
;
462 if (!info
|| info
->pair
< 0 || info
->pair
>= npairs
||
463 info
->group
< 0 || info
->group
>= ngroups
)
466 if (mtd
->pairing
&& mtd
->pairing
->get_wunit
)
467 return mtd
->pairing
->get_wunit(mtd
, info
);
471 EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit
);
474 * mtd_pairing_groups - get the number of pairing groups
475 * @mtd: pointer to new MTD device info structure
477 * Returns the number of pairing groups.
479 * This number is usually equal to the number of bits exposed by a single
480 * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
481 * to iterate over all pages of a given pair.
483 int mtd_pairing_groups(struct mtd_info
*mtd
)
485 if (!mtd
->pairing
|| !mtd
->pairing
->ngroups
)
488 return mtd
->pairing
->ngroups
;
490 EXPORT_SYMBOL_GPL(mtd_pairing_groups
);
492 static int mtd_nvmem_reg_read(void *priv
, unsigned int offset
,
493 void *val
, size_t bytes
)
495 struct mtd_info
*mtd
= priv
;
499 err
= mtd_read(mtd
, offset
, bytes
, &retlen
, val
);
500 if (err
&& err
!= -EUCLEAN
)
503 return retlen
== bytes
? 0 : -EIO
;
506 static int mtd_nvmem_add(struct mtd_info
*mtd
)
508 struct nvmem_config config
= {};
510 config
.dev
= &mtd
->dev
;
511 config
.name
= mtd
->name
;
512 config
.owner
= THIS_MODULE
;
513 config
.reg_read
= mtd_nvmem_reg_read
;
514 config
.size
= mtd
->size
;
515 config
.word_size
= 1;
517 config
.read_only
= true;
518 config
.root_only
= true;
519 config
.no_of_node
= true;
522 mtd
->nvmem
= nvmem_register(&config
);
523 if (IS_ERR(mtd
->nvmem
)) {
524 /* Just ignore if there is no NVMEM support in the kernel */
525 if (PTR_ERR(mtd
->nvmem
) == -ENOSYS
) {
528 dev_err(&mtd
->dev
, "Failed to register NVMEM device\n");
529 return PTR_ERR(mtd
->nvmem
);
536 static struct dentry
*dfs_dir_mtd
;
539 * add_mtd_device - register an MTD device
540 * @mtd: pointer to new MTD device info structure
542 * Add a device to the list of MTD devices present in the system, and
543 * notify each currently active MTD 'user' of its arrival. Returns
544 * zero on success or non-zero on failure.
547 int add_mtd_device(struct mtd_info
*mtd
)
549 struct mtd_notifier
*not;
553 * May occur, for instance, on buggy drivers which call
554 * mtd_device_parse_register() multiple times on the same master MTD,
555 * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
557 if (WARN_ONCE(mtd
->dev
.type
, "MTD already registered\n"))
560 BUG_ON(mtd
->writesize
== 0);
562 if (WARN_ON((!mtd
->erasesize
|| !mtd
->_erase
) &&
563 !(mtd
->flags
& MTD_NO_ERASE
)))
566 mutex_lock(&mtd_table_mutex
);
568 i
= idr_alloc(&mtd_idr
, mtd
, 0, 0, GFP_KERNEL
);
577 /* default value if not set by driver */
578 if (mtd
->bitflip_threshold
== 0)
579 mtd
->bitflip_threshold
= mtd
->ecc_strength
;
581 if (is_power_of_2(mtd
->erasesize
))
582 mtd
->erasesize_shift
= ffs(mtd
->erasesize
) - 1;
584 mtd
->erasesize_shift
= 0;
586 if (is_power_of_2(mtd
->writesize
))
587 mtd
->writesize_shift
= ffs(mtd
->writesize
) - 1;
589 mtd
->writesize_shift
= 0;
591 mtd
->erasesize_mask
= (1 << mtd
->erasesize_shift
) - 1;
592 mtd
->writesize_mask
= (1 << mtd
->writesize_shift
) - 1;
594 /* Some chips always power up locked. Unlock them now */
595 if ((mtd
->flags
& MTD_WRITEABLE
) && (mtd
->flags
& MTD_POWERUP_LOCK
)) {
596 error
= mtd_unlock(mtd
, 0, mtd
->size
);
597 if (error
&& error
!= -EOPNOTSUPP
)
599 "%s: unlock failed, writes may not work\n",
601 /* Ignore unlock failures? */
605 /* Caller should have set dev.parent to match the
606 * physical device, if appropriate.
608 mtd
->dev
.type
= &mtd_devtype
;
609 mtd
->dev
.class = &mtd_class
;
610 mtd
->dev
.devt
= MTD_DEVT(i
);
611 dev_set_name(&mtd
->dev
, "mtd%d", i
);
612 dev_set_drvdata(&mtd
->dev
, mtd
);
613 of_node_get(mtd_get_of_node(mtd
));
614 error
= device_register(&mtd
->dev
);
618 /* Add the nvmem provider */
619 error
= mtd_nvmem_add(mtd
);
623 if (!IS_ERR_OR_NULL(dfs_dir_mtd
)) {
624 mtd
->dbg
.dfs_dir
= debugfs_create_dir(dev_name(&mtd
->dev
), dfs_dir_mtd
);
625 if (IS_ERR_OR_NULL(mtd
->dbg
.dfs_dir
)) {
626 pr_debug("mtd device %s won't show data in debugfs\n",
627 dev_name(&mtd
->dev
));
631 device_create(&mtd_class
, mtd
->dev
.parent
, MTD_DEVT(i
) + 1, NULL
,
634 pr_debug("mtd: Giving out device %d to %s\n", i
, mtd
->name
);
635 /* No need to get a refcount on the module containing
636 the notifier, since we hold the mtd_table_mutex */
637 list_for_each_entry(not, &mtd_notifiers
, list
)
640 mutex_unlock(&mtd_table_mutex
);
641 /* We _know_ we aren't being removed, because
642 our caller is still holding us here. So none
643 of this try_ nonsense, and no bitching about it
645 __module_get(THIS_MODULE
);
649 device_unregister(&mtd
->dev
);
651 of_node_put(mtd_get_of_node(mtd
));
652 idr_remove(&mtd_idr
, i
);
654 mutex_unlock(&mtd_table_mutex
);
659 * del_mtd_device - unregister an MTD device
660 * @mtd: pointer to MTD device info structure
662 * Remove a device from the list of MTD devices present in the system,
663 * and notify each currently active MTD 'user' of its departure.
664 * Returns zero on success or 1 on failure, which currently will happen
665 * if the requested device does not appear to be present in the list.
668 int del_mtd_device(struct mtd_info
*mtd
)
671 struct mtd_notifier
*not;
673 mutex_lock(&mtd_table_mutex
);
675 debugfs_remove_recursive(mtd
->dbg
.dfs_dir
);
677 if (idr_find(&mtd_idr
, mtd
->index
) != mtd
) {
682 /* No need to get a refcount on the module containing
683 the notifier, since we hold the mtd_table_mutex */
684 list_for_each_entry(not, &mtd_notifiers
, list
)
688 printk(KERN_NOTICE
"Removing MTD device #%d (%s) with use count %d\n",
689 mtd
->index
, mtd
->name
, mtd
->usecount
);
692 /* Try to remove the NVMEM provider */
694 nvmem_unregister(mtd
->nvmem
);
696 device_unregister(&mtd
->dev
);
698 idr_remove(&mtd_idr
, mtd
->index
);
699 of_node_put(mtd_get_of_node(mtd
));
701 module_put(THIS_MODULE
);
706 mutex_unlock(&mtd_table_mutex
);
711 * Set a few defaults based on the parent devices, if not provided by the
714 static void mtd_set_dev_defaults(struct mtd_info
*mtd
)
716 if (mtd
->dev
.parent
) {
717 if (!mtd
->owner
&& mtd
->dev
.parent
->driver
)
718 mtd
->owner
= mtd
->dev
.parent
->driver
->owner
;
720 mtd
->name
= dev_name(mtd
->dev
.parent
);
722 pr_debug("mtd device won't show a device symlink in sysfs\n");
725 mtd
->orig_flags
= mtd
->flags
;
729 * mtd_device_parse_register - parse partitions and register an MTD device.
731 * @mtd: the MTD device to register
732 * @types: the list of MTD partition probes to try, see
733 * 'parse_mtd_partitions()' for more information
734 * @parser_data: MTD partition parser-specific data
735 * @parts: fallback partition information to register, if parsing fails;
736 * only valid if %nr_parts > %0
737 * @nr_parts: the number of partitions in parts, if zero then the full
738 * MTD device is registered if no partition info is found
740 * This function aggregates MTD partitions parsing (done by
741 * 'parse_mtd_partitions()') and MTD device and partitions registering. It
742 * basically follows the most common pattern found in many MTD drivers:
744 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
746 * * Then It tries to probe partitions on MTD device @mtd using parsers
747 * specified in @types (if @types is %NULL, then the default list of parsers
748 * is used, see 'parse_mtd_partitions()' for more information). If none are
749 * found this functions tries to fallback to information specified in
751 * * If no partitions were found this function just registers the MTD device
754 * Returns zero in case of success and a negative error code in case of failure.
756 int mtd_device_parse_register(struct mtd_info
*mtd
, const char * const *types
,
757 struct mtd_part_parser_data
*parser_data
,
758 const struct mtd_partition
*parts
,
763 mtd_set_dev_defaults(mtd
);
765 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER
)) {
766 ret
= add_mtd_device(mtd
);
771 /* Prefer parsed partitions over driver-provided fallback */
772 ret
= parse_mtd_partitions(mtd
, types
, parser_data
);
776 ret
= add_mtd_partitions(mtd
, parts
, nr_parts
);
777 else if (!device_is_registered(&mtd
->dev
))
778 ret
= add_mtd_device(mtd
);
786 * FIXME: some drivers unfortunately call this function more than once.
787 * So we have to check if we've already assigned the reboot notifier.
789 * Generally, we can make multiple calls work for most cases, but it
790 * does cause problems with parse_mtd_partitions() above (e.g.,
791 * cmdlineparts will register partitions more than once).
793 WARN_ONCE(mtd
->_reboot
&& mtd
->reboot_notifier
.notifier_call
,
794 "MTD already registered\n");
795 if (mtd
->_reboot
&& !mtd
->reboot_notifier
.notifier_call
) {
796 mtd
->reboot_notifier
.notifier_call
= mtd_reboot_notifier
;
797 register_reboot_notifier(&mtd
->reboot_notifier
);
801 if (ret
&& device_is_registered(&mtd
->dev
))
806 EXPORT_SYMBOL_GPL(mtd_device_parse_register
);
809 * mtd_device_unregister - unregister an existing MTD device.
811 * @master: the MTD device to unregister. This will unregister both the master
812 * and any partitions if registered.
814 int mtd_device_unregister(struct mtd_info
*master
)
819 unregister_reboot_notifier(&master
->reboot_notifier
);
821 err
= del_mtd_partitions(master
);
825 if (!device_is_registered(&master
->dev
))
828 return del_mtd_device(master
);
830 EXPORT_SYMBOL_GPL(mtd_device_unregister
);
833 * register_mtd_user - register a 'user' of MTD devices.
834 * @new: pointer to notifier info structure
836 * Registers a pair of callbacks function to be called upon addition
837 * or removal of MTD devices. Causes the 'add' callback to be immediately
838 * invoked for each MTD device currently present in the system.
840 void register_mtd_user (struct mtd_notifier
*new)
842 struct mtd_info
*mtd
;
844 mutex_lock(&mtd_table_mutex
);
846 list_add(&new->list
, &mtd_notifiers
);
848 __module_get(THIS_MODULE
);
850 mtd_for_each_device(mtd
)
853 mutex_unlock(&mtd_table_mutex
);
855 EXPORT_SYMBOL_GPL(register_mtd_user
);
858 * unregister_mtd_user - unregister a 'user' of MTD devices.
859 * @old: pointer to notifier info structure
861 * Removes a callback function pair from the list of 'users' to be
862 * notified upon addition or removal of MTD devices. Causes the
863 * 'remove' callback to be immediately invoked for each MTD device
864 * currently present in the system.
866 int unregister_mtd_user (struct mtd_notifier
*old
)
868 struct mtd_info
*mtd
;
870 mutex_lock(&mtd_table_mutex
);
872 module_put(THIS_MODULE
);
874 mtd_for_each_device(mtd
)
877 list_del(&old
->list
);
878 mutex_unlock(&mtd_table_mutex
);
881 EXPORT_SYMBOL_GPL(unregister_mtd_user
);
884 * get_mtd_device - obtain a validated handle for an MTD device
885 * @mtd: last known address of the required MTD device
886 * @num: internal device number of the required MTD device
888 * Given a number and NULL address, return the num'th entry in the device
889 * table, if any. Given an address and num == -1, search the device table
890 * for a device with that address and return if it's still present. Given
891 * both, return the num'th driver only if its address matches. Return
894 struct mtd_info
*get_mtd_device(struct mtd_info
*mtd
, int num
)
896 struct mtd_info
*ret
= NULL
, *other
;
899 mutex_lock(&mtd_table_mutex
);
902 mtd_for_each_device(other
) {
908 } else if (num
>= 0) {
909 ret
= idr_find(&mtd_idr
, num
);
910 if (mtd
&& mtd
!= ret
)
919 err
= __get_mtd_device(ret
);
923 mutex_unlock(&mtd_table_mutex
);
926 EXPORT_SYMBOL_GPL(get_mtd_device
);
929 int __get_mtd_device(struct mtd_info
*mtd
)
933 if (!try_module_get(mtd
->owner
))
936 if (mtd
->_get_device
) {
937 err
= mtd
->_get_device(mtd
);
940 module_put(mtd
->owner
);
947 EXPORT_SYMBOL_GPL(__get_mtd_device
);
950 * get_mtd_device_nm - obtain a validated handle for an MTD device by
952 * @name: MTD device name to open
954 * This function returns MTD device description structure in case of
955 * success and an error code in case of failure.
957 struct mtd_info
*get_mtd_device_nm(const char *name
)
960 struct mtd_info
*mtd
= NULL
, *other
;
962 mutex_lock(&mtd_table_mutex
);
964 mtd_for_each_device(other
) {
965 if (!strcmp(name
, other
->name
)) {
974 err
= __get_mtd_device(mtd
);
978 mutex_unlock(&mtd_table_mutex
);
982 mutex_unlock(&mtd_table_mutex
);
985 EXPORT_SYMBOL_GPL(get_mtd_device_nm
);
987 void put_mtd_device(struct mtd_info
*mtd
)
989 mutex_lock(&mtd_table_mutex
);
990 __put_mtd_device(mtd
);
991 mutex_unlock(&mtd_table_mutex
);
994 EXPORT_SYMBOL_GPL(put_mtd_device
);
996 void __put_mtd_device(struct mtd_info
*mtd
)
999 BUG_ON(mtd
->usecount
< 0);
1001 if (mtd
->_put_device
)
1002 mtd
->_put_device(mtd
);
1004 module_put(mtd
->owner
);
1006 EXPORT_SYMBOL_GPL(__put_mtd_device
);
1009 * Erase is an synchronous operation. Device drivers are epected to return a
1010 * negative error code if the operation failed and update instr->fail_addr
1011 * to point the portion that was not properly erased.
1013 int mtd_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
1015 instr
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
1017 if (!mtd
->erasesize
|| !mtd
->_erase
)
1020 if (instr
->addr
>= mtd
->size
|| instr
->len
> mtd
->size
- instr
->addr
)
1022 if (!(mtd
->flags
& MTD_WRITEABLE
))
1028 ledtrig_mtd_activity();
1029 return mtd
->_erase(mtd
, instr
);
1031 EXPORT_SYMBOL_GPL(mtd_erase
);
1034 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
1036 int mtd_point(struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
,
1037 void **virt
, resource_size_t
*phys
)
1045 if (from
< 0 || from
>= mtd
->size
|| len
> mtd
->size
- from
)
1049 return mtd
->_point(mtd
, from
, len
, retlen
, virt
, phys
);
1051 EXPORT_SYMBOL_GPL(mtd_point
);
1053 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
1054 int mtd_unpoint(struct mtd_info
*mtd
, loff_t from
, size_t len
)
1058 if (from
< 0 || from
>= mtd
->size
|| len
> mtd
->size
- from
)
1062 return mtd
->_unpoint(mtd
, from
, len
);
1064 EXPORT_SYMBOL_GPL(mtd_unpoint
);
1067 * Allow NOMMU mmap() to directly map the device (if not NULL)
1068 * - return the address to which the offset maps
1069 * - return -ENOSYS to indicate refusal to do the mapping
1071 unsigned long mtd_get_unmapped_area(struct mtd_info
*mtd
, unsigned long len
,
1072 unsigned long offset
, unsigned long flags
)
1078 ret
= mtd_point(mtd
, offset
, len
, &retlen
, &virt
, NULL
);
1081 if (retlen
!= len
) {
1082 mtd_unpoint(mtd
, offset
, retlen
);
1085 return (unsigned long)virt
;
1087 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area
);
1089 int mtd_read(struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
,
1094 if (from
< 0 || from
>= mtd
->size
|| len
> mtd
->size
- from
)
1099 ledtrig_mtd_activity();
1101 * In the absence of an error, drivers return a non-negative integer
1102 * representing the maximum number of bitflips that were corrected on
1103 * any one ecc region (if applicable; zero otherwise).
1106 ret_code
= mtd
->_read(mtd
, from
, len
, retlen
, buf
);
1107 } else if (mtd
->_read_oob
) {
1108 struct mtd_oob_ops ops
= {
1113 ret_code
= mtd
->_read_oob(mtd
, from
, &ops
);
1114 *retlen
= ops
.retlen
;
1119 if (unlikely(ret_code
< 0))
1121 if (mtd
->ecc_strength
== 0)
1122 return 0; /* device lacks ecc */
1123 return ret_code
>= mtd
->bitflip_threshold
? -EUCLEAN
: 0;
1125 EXPORT_SYMBOL_GPL(mtd_read
);
1127 int mtd_write(struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
,
1131 if (to
< 0 || to
>= mtd
->size
|| len
> mtd
->size
- to
)
1133 if ((!mtd
->_write
&& !mtd
->_write_oob
) ||
1134 !(mtd
->flags
& MTD_WRITEABLE
))
1138 ledtrig_mtd_activity();
1141 struct mtd_oob_ops ops
= {
1143 .datbuf
= (u8
*)buf
,
1147 ret
= mtd
->_write_oob(mtd
, to
, &ops
);
1148 *retlen
= ops
.retlen
;
1152 return mtd
->_write(mtd
, to
, len
, retlen
, buf
);
1154 EXPORT_SYMBOL_GPL(mtd_write
);
1157 * In blackbox flight recorder like scenarios we want to make successful writes
1158 * in interrupt context. panic_write() is only intended to be called when its
1159 * known the kernel is about to panic and we need the write to succeed. Since
1160 * the kernel is not going to be running for much longer, this function can
1161 * break locks and delay to ensure the write succeeds (but not sleep).
1163 int mtd_panic_write(struct mtd_info
*mtd
, loff_t to
, size_t len
, size_t *retlen
,
1167 if (!mtd
->_panic_write
)
1169 if (to
< 0 || to
>= mtd
->size
|| len
> mtd
->size
- to
)
1171 if (!(mtd
->flags
& MTD_WRITEABLE
))
1175 return mtd
->_panic_write(mtd
, to
, len
, retlen
, buf
);
1177 EXPORT_SYMBOL_GPL(mtd_panic_write
);
1179 static int mtd_check_oob_ops(struct mtd_info
*mtd
, loff_t offs
,
1180 struct mtd_oob_ops
*ops
)
1183 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
1184 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
1193 if (offs
< 0 || offs
+ ops
->len
> mtd
->size
)
1199 if (ops
->ooboffs
>= mtd_oobavail(mtd
, ops
))
1202 maxooblen
= ((size_t)(mtd_div_by_ws(mtd
->size
, mtd
) -
1203 mtd_div_by_ws(offs
, mtd
)) *
1204 mtd_oobavail(mtd
, ops
)) - ops
->ooboffs
;
1205 if (ops
->ooblen
> maxooblen
)
1212 int mtd_read_oob(struct mtd_info
*mtd
, loff_t from
, struct mtd_oob_ops
*ops
)
1215 ops
->retlen
= ops
->oobretlen
= 0;
1217 ret_code
= mtd_check_oob_ops(mtd
, from
, ops
);
1221 ledtrig_mtd_activity();
1223 /* Check the validity of a potential fallback on mtd->_read */
1224 if (!mtd
->_read_oob
&& (!mtd
->_read
|| ops
->oobbuf
))
1228 ret_code
= mtd
->_read_oob(mtd
, from
, ops
);
1230 ret_code
= mtd
->_read(mtd
, from
, ops
->len
, &ops
->retlen
,
1234 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
1235 * similar to mtd->_read(), returning a non-negative integer
1236 * representing max bitflips. In other cases, mtd->_read_oob() may
1237 * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
1239 if (unlikely(ret_code
< 0))
1241 if (mtd
->ecc_strength
== 0)
1242 return 0; /* device lacks ecc */
1243 return ret_code
>= mtd
->bitflip_threshold
? -EUCLEAN
: 0;
1245 EXPORT_SYMBOL_GPL(mtd_read_oob
);
1247 int mtd_write_oob(struct mtd_info
*mtd
, loff_t to
,
1248 struct mtd_oob_ops
*ops
)
1252 ops
->retlen
= ops
->oobretlen
= 0;
1254 if (!(mtd
->flags
& MTD_WRITEABLE
))
1257 ret
= mtd_check_oob_ops(mtd
, to
, ops
);
1261 ledtrig_mtd_activity();
1263 /* Check the validity of a potential fallback on mtd->_write */
1264 if (!mtd
->_write_oob
&& (!mtd
->_write
|| ops
->oobbuf
))
1267 if (mtd
->_write_oob
)
1268 return mtd
->_write_oob(mtd
, to
, ops
);
1270 return mtd
->_write(mtd
, to
, ops
->len
, &ops
->retlen
,
1273 EXPORT_SYMBOL_GPL(mtd_write_oob
);
1276 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
1277 * @mtd: MTD device structure
1278 * @section: ECC section. Depending on the layout you may have all the ECC
1279 * bytes stored in a single contiguous section, or one section
1280 * per ECC chunk (and sometime several sections for a single ECC
1282 * @oobecc: OOB region struct filled with the appropriate ECC position
1285 * This function returns ECC section information in the OOB area. If you want
1286 * to get all the ECC bytes information, then you should call
1287 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
1289 * Returns zero on success, a negative error code otherwise.
1291 int mtd_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
1292 struct mtd_oob_region
*oobecc
)
1294 memset(oobecc
, 0, sizeof(*oobecc
));
1296 if (!mtd
|| section
< 0)
1299 if (!mtd
->ooblayout
|| !mtd
->ooblayout
->ecc
)
1302 return mtd
->ooblayout
->ecc(mtd
, section
, oobecc
);
1304 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc
);
1307 * mtd_ooblayout_free - Get the OOB region definition of a specific free
1309 * @mtd: MTD device structure
1310 * @section: Free section you are interested in. Depending on the layout
1311 * you may have all the free bytes stored in a single contiguous
1312 * section, or one section per ECC chunk plus an extra section
1313 * for the remaining bytes (or other funky layout).
1314 * @oobfree: OOB region struct filled with the appropriate free position
1317 * This function returns free bytes position in the OOB area. If you want
1318 * to get all the free bytes information, then you should call
1319 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
1321 * Returns zero on success, a negative error code otherwise.
1323 int mtd_ooblayout_free(struct mtd_info
*mtd
, int section
,
1324 struct mtd_oob_region
*oobfree
)
1326 memset(oobfree
, 0, sizeof(*oobfree
));
1328 if (!mtd
|| section
< 0)
1331 if (!mtd
->ooblayout
|| !mtd
->ooblayout
->free
)
1334 return mtd
->ooblayout
->free(mtd
, section
, oobfree
);
1336 EXPORT_SYMBOL_GPL(mtd_ooblayout_free
);
1339 * mtd_ooblayout_find_region - Find the region attached to a specific byte
1340 * @mtd: mtd info structure
1341 * @byte: the byte we are searching for
1342 * @sectionp: pointer where the section id will be stored
1343 * @oobregion: used to retrieve the ECC position
1344 * @iter: iterator function. Should be either mtd_ooblayout_free or
1345 * mtd_ooblayout_ecc depending on the region type you're searching for
1347 * This function returns the section id and oobregion information of a
1348 * specific byte. For example, say you want to know where the 4th ECC byte is
1349 * stored, you'll use:
1351 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc);
1353 * Returns zero on success, a negative error code otherwise.
1355 static int mtd_ooblayout_find_region(struct mtd_info
*mtd
, int byte
,
1356 int *sectionp
, struct mtd_oob_region
*oobregion
,
1357 int (*iter
)(struct mtd_info
*,
1359 struct mtd_oob_region
*oobregion
))
1361 int pos
= 0, ret
, section
= 0;
1363 memset(oobregion
, 0, sizeof(*oobregion
));
1366 ret
= iter(mtd
, section
, oobregion
);
1370 if (pos
+ oobregion
->length
> byte
)
1373 pos
+= oobregion
->length
;
1378 * Adjust region info to make it start at the beginning at the
1381 oobregion
->offset
+= byte
- pos
;
1382 oobregion
->length
-= byte
- pos
;
1383 *sectionp
= section
;
1389 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
1391 * @mtd: mtd info structure
1392 * @eccbyte: the byte we are searching for
1393 * @sectionp: pointer where the section id will be stored
1394 * @oobregion: OOB region information
1396 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
1399 * Returns zero on success, a negative error code otherwise.
1401 int mtd_ooblayout_find_eccregion(struct mtd_info
*mtd
, int eccbyte
,
1403 struct mtd_oob_region
*oobregion
)
1405 return mtd_ooblayout_find_region(mtd
, eccbyte
, section
, oobregion
,
1408 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion
);
1411 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
1412 * @mtd: mtd info structure
1413 * @buf: destination buffer to store OOB bytes
1414 * @oobbuf: OOB buffer
1415 * @start: first byte to retrieve
1416 * @nbytes: number of bytes to retrieve
1417 * @iter: section iterator
1419 * Extract bytes attached to a specific category (ECC or free)
1420 * from the OOB buffer and copy them into buf.
1422 * Returns zero on success, a negative error code otherwise.
1424 static int mtd_ooblayout_get_bytes(struct mtd_info
*mtd
, u8
*buf
,
1425 const u8
*oobbuf
, int start
, int nbytes
,
1426 int (*iter
)(struct mtd_info
*,
1428 struct mtd_oob_region
*oobregion
))
1430 struct mtd_oob_region oobregion
;
1433 ret
= mtd_ooblayout_find_region(mtd
, start
, §ion
,
1439 cnt
= min_t(int, nbytes
, oobregion
.length
);
1440 memcpy(buf
, oobbuf
+ oobregion
.offset
, cnt
);
1447 ret
= iter(mtd
, ++section
, &oobregion
);
1454 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
1455 * @mtd: mtd info structure
1456 * @buf: source buffer to get OOB bytes from
1457 * @oobbuf: OOB buffer
1458 * @start: first OOB byte to set
1459 * @nbytes: number of OOB bytes to set
1460 * @iter: section iterator
1462 * Fill the OOB buffer with data provided in buf. The category (ECC or free)
1463 * is selected by passing the appropriate iterator.
1465 * Returns zero on success, a negative error code otherwise.
1467 static int mtd_ooblayout_set_bytes(struct mtd_info
*mtd
, const u8
*buf
,
1468 u8
*oobbuf
, int start
, int nbytes
,
1469 int (*iter
)(struct mtd_info
*,
1471 struct mtd_oob_region
*oobregion
))
1473 struct mtd_oob_region oobregion
;
1476 ret
= mtd_ooblayout_find_region(mtd
, start
, §ion
,
1482 cnt
= min_t(int, nbytes
, oobregion
.length
);
1483 memcpy(oobbuf
+ oobregion
.offset
, buf
, cnt
);
1490 ret
= iter(mtd
, ++section
, &oobregion
);
1497 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
1498 * @mtd: mtd info structure
1499 * @iter: category iterator
1501 * Count the number of bytes in a given category.
1503 * Returns a positive value on success, a negative error code otherwise.
1505 static int mtd_ooblayout_count_bytes(struct mtd_info
*mtd
,
1506 int (*iter
)(struct mtd_info
*,
1508 struct mtd_oob_region
*oobregion
))
1510 struct mtd_oob_region oobregion
;
1511 int section
= 0, ret
, nbytes
= 0;
1514 ret
= iter(mtd
, section
++, &oobregion
);
1521 nbytes
+= oobregion
.length
;
1528 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
1529 * @mtd: mtd info structure
1530 * @eccbuf: destination buffer to store ECC bytes
1531 * @oobbuf: OOB buffer
1532 * @start: first ECC byte to retrieve
1533 * @nbytes: number of ECC bytes to retrieve
1535 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
1537 * Returns zero on success, a negative error code otherwise.
1539 int mtd_ooblayout_get_eccbytes(struct mtd_info
*mtd
, u8
*eccbuf
,
1540 const u8
*oobbuf
, int start
, int nbytes
)
1542 return mtd_ooblayout_get_bytes(mtd
, eccbuf
, oobbuf
, start
, nbytes
,
1545 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes
);
1548 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
1549 * @mtd: mtd info structure
1550 * @eccbuf: source buffer to get ECC bytes from
1551 * @oobbuf: OOB buffer
1552 * @start: first ECC byte to set
1553 * @nbytes: number of ECC bytes to set
1555 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
1557 * Returns zero on success, a negative error code otherwise.
1559 int mtd_ooblayout_set_eccbytes(struct mtd_info
*mtd
, const u8
*eccbuf
,
1560 u8
*oobbuf
, int start
, int nbytes
)
1562 return mtd_ooblayout_set_bytes(mtd
, eccbuf
, oobbuf
, start
, nbytes
,
1565 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes
);
1568 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
1569 * @mtd: mtd info structure
1570 * @databuf: destination buffer to store ECC bytes
1571 * @oobbuf: OOB buffer
1572 * @start: first ECC byte to retrieve
1573 * @nbytes: number of ECC bytes to retrieve
1575 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1577 * Returns zero on success, a negative error code otherwise.
1579 int mtd_ooblayout_get_databytes(struct mtd_info
*mtd
, u8
*databuf
,
1580 const u8
*oobbuf
, int start
, int nbytes
)
1582 return mtd_ooblayout_get_bytes(mtd
, databuf
, oobbuf
, start
, nbytes
,
1583 mtd_ooblayout_free
);
1585 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes
);
1588 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
1589 * @mtd: mtd info structure
1590 * @databuf: source buffer to get data bytes from
1591 * @oobbuf: OOB buffer
1592 * @start: first ECC byte to set
1593 * @nbytes: number of ECC bytes to set
1595 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
1597 * Returns zero on success, a negative error code otherwise.
1599 int mtd_ooblayout_set_databytes(struct mtd_info
*mtd
, const u8
*databuf
,
1600 u8
*oobbuf
, int start
, int nbytes
)
1602 return mtd_ooblayout_set_bytes(mtd
, databuf
, oobbuf
, start
, nbytes
,
1603 mtd_ooblayout_free
);
1605 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes
);
1608 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
1609 * @mtd: mtd info structure
1611 * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
1613 * Returns zero on success, a negative error code otherwise.
1615 int mtd_ooblayout_count_freebytes(struct mtd_info
*mtd
)
1617 return mtd_ooblayout_count_bytes(mtd
, mtd_ooblayout_free
);
1619 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes
);
1622 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
1623 * @mtd: mtd info structure
1625 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
1627 * Returns zero on success, a negative error code otherwise.
1629 int mtd_ooblayout_count_eccbytes(struct mtd_info
*mtd
)
1631 return mtd_ooblayout_count_bytes(mtd
, mtd_ooblayout_ecc
);
1633 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes
);
1636 * Method to access the protection register area, present in some flash
1637 * devices. The user data is one time programmable but the factory data is read
1640 int mtd_get_fact_prot_info(struct mtd_info
*mtd
, size_t len
, size_t *retlen
,
1641 struct otp_info
*buf
)
1643 if (!mtd
->_get_fact_prot_info
)
1647 return mtd
->_get_fact_prot_info(mtd
, len
, retlen
, buf
);
1649 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info
);
1651 int mtd_read_fact_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1652 size_t *retlen
, u_char
*buf
)
1655 if (!mtd
->_read_fact_prot_reg
)
1659 return mtd
->_read_fact_prot_reg(mtd
, from
, len
, retlen
, buf
);
1661 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg
);
1663 int mtd_get_user_prot_info(struct mtd_info
*mtd
, size_t len
, size_t *retlen
,
1664 struct otp_info
*buf
)
1666 if (!mtd
->_get_user_prot_info
)
1670 return mtd
->_get_user_prot_info(mtd
, len
, retlen
, buf
);
1672 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info
);
1674 int mtd_read_user_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1675 size_t *retlen
, u_char
*buf
)
1678 if (!mtd
->_read_user_prot_reg
)
1682 return mtd
->_read_user_prot_reg(mtd
, from
, len
, retlen
, buf
);
1684 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg
);
1686 int mtd_write_user_prot_reg(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1687 size_t *retlen
, u_char
*buf
)
1692 if (!mtd
->_write_user_prot_reg
)
1696 ret
= mtd
->_write_user_prot_reg(mtd
, to
, len
, retlen
, buf
);
1701 * If no data could be written at all, we are out of memory and
1702 * must return -ENOSPC.
1704 return (*retlen
) ? 0 : -ENOSPC
;
1706 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg
);
1708 int mtd_lock_user_prot_reg(struct mtd_info
*mtd
, loff_t from
, size_t len
)
1710 if (!mtd
->_lock_user_prot_reg
)
1714 return mtd
->_lock_user_prot_reg(mtd
, from
, len
);
1716 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg
);
1718 /* Chip-supported device locking */
1719 int mtd_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1723 if (ofs
< 0 || ofs
>= mtd
->size
|| len
> mtd
->size
- ofs
)
1727 return mtd
->_lock(mtd
, ofs
, len
);
1729 EXPORT_SYMBOL_GPL(mtd_lock
);
1731 int mtd_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1735 if (ofs
< 0 || ofs
>= mtd
->size
|| len
> mtd
->size
- ofs
)
1739 return mtd
->_unlock(mtd
, ofs
, len
);
1741 EXPORT_SYMBOL_GPL(mtd_unlock
);
1743 int mtd_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1745 if (!mtd
->_is_locked
)
1747 if (ofs
< 0 || ofs
>= mtd
->size
|| len
> mtd
->size
- ofs
)
1751 return mtd
->_is_locked(mtd
, ofs
, len
);
1753 EXPORT_SYMBOL_GPL(mtd_is_locked
);
1755 int mtd_block_isreserved(struct mtd_info
*mtd
, loff_t ofs
)
1757 if (ofs
< 0 || ofs
>= mtd
->size
)
1759 if (!mtd
->_block_isreserved
)
1761 return mtd
->_block_isreserved(mtd
, ofs
);
1763 EXPORT_SYMBOL_GPL(mtd_block_isreserved
);
1765 int mtd_block_isbad(struct mtd_info
*mtd
, loff_t ofs
)
1767 if (ofs
< 0 || ofs
>= mtd
->size
)
1769 if (!mtd
->_block_isbad
)
1771 return mtd
->_block_isbad(mtd
, ofs
);
1773 EXPORT_SYMBOL_GPL(mtd_block_isbad
);
1775 int mtd_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
1777 if (!mtd
->_block_markbad
)
1779 if (ofs
< 0 || ofs
>= mtd
->size
)
1781 if (!(mtd
->flags
& MTD_WRITEABLE
))
1783 return mtd
->_block_markbad(mtd
, ofs
);
1785 EXPORT_SYMBOL_GPL(mtd_block_markbad
);
1788 * default_mtd_writev - the default writev method
1789 * @mtd: mtd device description object pointer
1790 * @vecs: the vectors to write
1791 * @count: count of vectors in @vecs
1792 * @to: the MTD device offset to write to
1793 * @retlen: on exit contains the count of bytes written to the MTD device.
1795 * This function returns zero in case of success and a negative error code in
1798 static int default_mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1799 unsigned long count
, loff_t to
, size_t *retlen
)
1802 size_t totlen
= 0, thislen
;
1805 for (i
= 0; i
< count
; i
++) {
1806 if (!vecs
[i
].iov_len
)
1808 ret
= mtd_write(mtd
, to
, vecs
[i
].iov_len
, &thislen
,
1811 if (ret
|| thislen
!= vecs
[i
].iov_len
)
1813 to
+= vecs
[i
].iov_len
;
1820 * mtd_writev - the vector-based MTD write method
1821 * @mtd: mtd device description object pointer
1822 * @vecs: the vectors to write
1823 * @count: count of vectors in @vecs
1824 * @to: the MTD device offset to write to
1825 * @retlen: on exit contains the count of bytes written to the MTD device.
1827 * This function returns zero in case of success and a negative error code in
1830 int mtd_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
1831 unsigned long count
, loff_t to
, size_t *retlen
)
1834 if (!(mtd
->flags
& MTD_WRITEABLE
))
1837 return default_mtd_writev(mtd
, vecs
, count
, to
, retlen
);
1838 return mtd
->_writev(mtd
, vecs
, count
, to
, retlen
);
1840 EXPORT_SYMBOL_GPL(mtd_writev
);
1843 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
1844 * @mtd: mtd device description object pointer
1845 * @size: a pointer to the ideal or maximum size of the allocation, points
1846 * to the actual allocation size on success.
1848 * This routine attempts to allocate a contiguous kernel buffer up to
1849 * the specified size, backing off the size of the request exponentially
1850 * until the request succeeds or until the allocation size falls below
1851 * the system page size. This attempts to make sure it does not adversely
1852 * impact system performance, so when allocating more than one page, we
1853 * ask the memory allocator to avoid re-trying, swapping, writing back
1854 * or performing I/O.
1856 * Note, this function also makes sure that the allocated buffer is aligned to
1857 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
1859 * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
1860 * to handle smaller (i.e. degraded) buffer allocations under low- or
1861 * fragmented-memory situations where such reduced allocations, from a
1862 * requested ideal, are allowed.
1864 * Returns a pointer to the allocated buffer on success; otherwise, NULL.
1866 void *mtd_kmalloc_up_to(const struct mtd_info
*mtd
, size_t *size
)
1868 gfp_t flags
= __GFP_NOWARN
| __GFP_DIRECT_RECLAIM
| __GFP_NORETRY
;
1869 size_t min_alloc
= max_t(size_t, mtd
->writesize
, PAGE_SIZE
);
1872 *size
= min_t(size_t, *size
, KMALLOC_MAX_SIZE
);
1874 while (*size
> min_alloc
) {
1875 kbuf
= kmalloc(*size
, flags
);
1880 *size
= ALIGN(*size
, mtd
->writesize
);
1884 * For the last resort allocation allow 'kmalloc()' to do all sorts of
1885 * things (write-back, dropping caches, etc) by using GFP_KERNEL.
1887 return kmalloc(*size
, GFP_KERNEL
);
1889 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to
);
1891 #ifdef CONFIG_PROC_FS
1893 /*====================================================================*/
1894 /* Support for /proc/mtd */
1896 static int mtd_proc_show(struct seq_file
*m
, void *v
)
1898 struct mtd_info
*mtd
;
1900 seq_puts(m
, "dev: size erasesize name\n");
1901 mutex_lock(&mtd_table_mutex
);
1902 mtd_for_each_device(mtd
) {
1903 seq_printf(m
, "mtd%d: %8.8llx %8.8x \"%s\"\n",
1904 mtd
->index
, (unsigned long long)mtd
->size
,
1905 mtd
->erasesize
, mtd
->name
);
1907 mutex_unlock(&mtd_table_mutex
);
1910 #endif /* CONFIG_PROC_FS */
1912 /*====================================================================*/
1915 static struct backing_dev_info
* __init
mtd_bdi_init(char *name
)
1917 struct backing_dev_info
*bdi
;
1920 bdi
= bdi_alloc(GFP_KERNEL
);
1922 return ERR_PTR(-ENOMEM
);
1926 * We put '-0' suffix to the name to get the same name format as we
1927 * used to get. Since this is called only once, we get a unique name.
1929 ret
= bdi_register(bdi
, "%.28s-0", name
);
1933 return ret
? ERR_PTR(ret
) : bdi
;
1936 static struct proc_dir_entry
*proc_mtd
;
1938 static int __init
init_mtd(void)
1942 ret
= class_register(&mtd_class
);
1946 mtd_bdi
= mtd_bdi_init("mtd");
1947 if (IS_ERR(mtd_bdi
)) {
1948 ret
= PTR_ERR(mtd_bdi
);
1952 proc_mtd
= proc_create_single("mtd", 0, NULL
, mtd_proc_show
);
1954 ret
= init_mtdchar();
1958 dfs_dir_mtd
= debugfs_create_dir("mtd", NULL
);
1964 remove_proc_entry("mtd", NULL
);
1967 class_unregister(&mtd_class
);
1969 pr_err("Error registering mtd class or bdi: %d\n", ret
);
1973 static void __exit
cleanup_mtd(void)
1975 debugfs_remove_recursive(dfs_dir_mtd
);
1978 remove_proc_entry("mtd", NULL
);
1979 class_unregister(&mtd_class
);
1981 idr_destroy(&mtd_idr
);
1984 module_init(init_mtd
);
1985 module_exit(cleanup_mtd
);
1987 MODULE_LICENSE("GPL");
1988 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1989 MODULE_DESCRIPTION("Core MTD registration and access routines");