1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Core registration and callback routines for MTD 4 * drivers and users. 5 * 6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> 7 * Copyright © 2006 Red Hat UK Limited 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/ptrace.h> 13 #include <linux/seq_file.h> 14 #include <linux/string.h> 15 #include <linux/timer.h> 16 #include <linux/major.h> 17 #include <linux/fs.h> 18 #include <linux/err.h> 19 #include <linux/ioctl.h> 20 #include <linux/init.h> 21 #include <linux/of.h> 22 #include <linux/proc_fs.h> 23 #include <linux/idr.h> 24 #include <linux/backing-dev.h> 25 #include <linux/gfp.h> 26 #include <linux/random.h> 27 #include <linux/slab.h> 28 #include <linux/reboot.h> 29 #include <linux/leds.h> 30 #include <linux/debugfs.h> 31 #include <linux/nvmem-provider.h> 32 #include <linux/root_dev.h> 33 #include <linux/error-injection.h> 34 35 #include <linux/mtd/mtd.h> 36 #include <linux/mtd/partitions.h> 37 #include <linux/mtd/concat.h> 38 39 #include "mtdcore.h" 40 41 struct backing_dev_info *mtd_bdi; 42 43 #ifdef CONFIG_PM_SLEEP 44 45 static int mtd_cls_suspend(struct device *dev) 46 { 47 struct mtd_info *mtd = dev_get_drvdata(dev); 48 49 return mtd ? mtd_suspend(mtd) : 0; 50 } 51 52 static int mtd_cls_resume(struct device *dev) 53 { 54 struct mtd_info *mtd = dev_get_drvdata(dev); 55 56 if (mtd) 57 mtd_resume(mtd); 58 return 0; 59 } 60 61 static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume); 62 #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops) 63 #else 64 #define MTD_CLS_PM_OPS NULL 65 #endif 66 67 static struct class mtd_class = { 68 .name = "mtd", 69 .pm = MTD_CLS_PM_OPS, 70 }; 71 72 static DEFINE_IDR(mtd_idr); 73 74 /* These are exported solely for the purpose of mtd_blkdevs.c. You 75 should not use them for _anything_ else */ 76 DEFINE_MUTEX(mtd_table_mutex); 77 EXPORT_SYMBOL_GPL(mtd_table_mutex); 78 79 struct mtd_info *__mtd_next_device(int i) 80 { 81 return idr_get_next(&mtd_idr, &i); 82 } 83 EXPORT_SYMBOL_GPL(__mtd_next_device); 84 85 static LIST_HEAD(mtd_notifiers); 86 87 88 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) 89 90 /* REVISIT once MTD uses the driver model better, whoever allocates 91 * the mtd_info will probably want to use the release() hook... 92 */ 93 static void mtd_release(struct device *dev) 94 { 95 struct mtd_info *mtd = dev_get_drvdata(dev); 96 dev_t index = MTD_DEVT(mtd->index); 97 98 idr_remove(&mtd_idr, mtd->index); 99 of_node_put(mtd_get_of_node(mtd)); 100 101 if (mtd_is_partition(mtd)) 102 release_mtd_partition(mtd); 103 104 /* remove /dev/mtdXro node */ 105 device_destroy(&mtd_class, index + 1); 106 } 107 108 /* 109 * No-op device release used in add_mtd_device() error paths. 110 * Prevents mtd_release() from being called via device_release(), 111 * which would free the mtd_info that the caller still manages. 112 */ 113 static void mtd_dev_release_nop(struct device *dev) 114 { 115 } 116 117 static void mtd_device_release(struct kref *kref) 118 { 119 struct mtd_info *mtd = container_of(kref, struct mtd_info, refcnt); 120 bool is_partition = mtd_is_partition(mtd); 121 122 debugfs_remove_recursive(mtd->dbg.dfs_dir); 123 124 /* Try to remove the NVMEM provider */ 125 nvmem_unregister(mtd->nvmem); 126 127 device_unregister(&mtd->dev); 128 129 /* 130 * Clear dev so mtd can be safely re-registered later if desired. 131 * Should not be done for partition, 132 * as it was already destroyed in device_unregister(). 133 */ 134 if (!is_partition) 135 memset(&mtd->dev, 0, sizeof(mtd->dev)); 136 137 module_put(THIS_MODULE); 138 } 139 140 #define MTD_DEVICE_ATTR_RO(name) \ 141 static DEVICE_ATTR(name, 0444, mtd_##name##_show, NULL) 142 143 #define MTD_DEVICE_ATTR_RW(name) \ 144 static DEVICE_ATTR(name, 0644, mtd_##name##_show, mtd_##name##_store) 145 146 static ssize_t mtd_type_show(struct device *dev, 147 struct device_attribute *attr, char *buf) 148 { 149 struct mtd_info *mtd = dev_get_drvdata(dev); 150 char *type; 151 152 switch (mtd->type) { 153 case MTD_ABSENT: 154 type = "absent"; 155 break; 156 case MTD_RAM: 157 type = "ram"; 158 break; 159 case MTD_ROM: 160 type = "rom"; 161 break; 162 case MTD_NORFLASH: 163 type = "nor"; 164 break; 165 case MTD_NANDFLASH: 166 type = "nand"; 167 break; 168 case MTD_DATAFLASH: 169 type = "dataflash"; 170 break; 171 case MTD_UBIVOLUME: 172 type = "ubi"; 173 break; 174 case MTD_MLCNANDFLASH: 175 type = "mlc-nand"; 176 break; 177 default: 178 type = "unknown"; 179 } 180 181 return sysfs_emit(buf, "%s\n", type); 182 } 183 MTD_DEVICE_ATTR_RO(type); 184 185 static ssize_t mtd_flags_show(struct device *dev, 186 struct device_attribute *attr, char *buf) 187 { 188 struct mtd_info *mtd = dev_get_drvdata(dev); 189 190 return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags); 191 } 192 MTD_DEVICE_ATTR_RO(flags); 193 194 static ssize_t mtd_size_show(struct device *dev, 195 struct device_attribute *attr, char *buf) 196 { 197 struct mtd_info *mtd = dev_get_drvdata(dev); 198 199 return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size); 200 } 201 MTD_DEVICE_ATTR_RO(size); 202 203 static ssize_t mtd_erasesize_show(struct device *dev, 204 struct device_attribute *attr, char *buf) 205 { 206 struct mtd_info *mtd = dev_get_drvdata(dev); 207 208 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize); 209 } 210 MTD_DEVICE_ATTR_RO(erasesize); 211 212 static ssize_t mtd_writesize_show(struct device *dev, 213 struct device_attribute *attr, char *buf) 214 { 215 struct mtd_info *mtd = dev_get_drvdata(dev); 216 217 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize); 218 } 219 MTD_DEVICE_ATTR_RO(writesize); 220 221 static ssize_t mtd_subpagesize_show(struct device *dev, 222 struct device_attribute *attr, char *buf) 223 { 224 struct mtd_info *mtd = dev_get_drvdata(dev); 225 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; 226 227 return sysfs_emit(buf, "%u\n", subpagesize); 228 } 229 MTD_DEVICE_ATTR_RO(subpagesize); 230 231 static ssize_t mtd_oobsize_show(struct device *dev, 232 struct device_attribute *attr, char *buf) 233 { 234 struct mtd_info *mtd = dev_get_drvdata(dev); 235 236 return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize); 237 } 238 MTD_DEVICE_ATTR_RO(oobsize); 239 240 static ssize_t mtd_oobavail_show(struct device *dev, 241 struct device_attribute *attr, char *buf) 242 { 243 struct mtd_info *mtd = dev_get_drvdata(dev); 244 245 return sysfs_emit(buf, "%u\n", mtd->oobavail); 246 } 247 MTD_DEVICE_ATTR_RO(oobavail); 248 249 static ssize_t mtd_numeraseregions_show(struct device *dev, 250 struct device_attribute *attr, char *buf) 251 { 252 struct mtd_info *mtd = dev_get_drvdata(dev); 253 254 return sysfs_emit(buf, "%u\n", mtd->numeraseregions); 255 } 256 MTD_DEVICE_ATTR_RO(numeraseregions); 257 258 static ssize_t mtd_name_show(struct device *dev, 259 struct device_attribute *attr, char *buf) 260 { 261 struct mtd_info *mtd = dev_get_drvdata(dev); 262 263 return sysfs_emit(buf, "%s\n", mtd->name); 264 } 265 MTD_DEVICE_ATTR_RO(name); 266 267 static ssize_t mtd_ecc_strength_show(struct device *dev, 268 struct device_attribute *attr, char *buf) 269 { 270 struct mtd_info *mtd = dev_get_drvdata(dev); 271 272 return sysfs_emit(buf, "%u\n", mtd->ecc_strength); 273 } 274 MTD_DEVICE_ATTR_RO(ecc_strength); 275 276 static ssize_t mtd_bitflip_threshold_show(struct device *dev, 277 struct device_attribute *attr, 278 char *buf) 279 { 280 struct mtd_info *mtd = dev_get_drvdata(dev); 281 282 return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold); 283 } 284 285 static ssize_t mtd_bitflip_threshold_store(struct device *dev, 286 struct device_attribute *attr, 287 const char *buf, size_t count) 288 { 289 struct mtd_info *mtd = dev_get_drvdata(dev); 290 unsigned int bitflip_threshold; 291 int retval; 292 293 retval = kstrtouint(buf, 0, &bitflip_threshold); 294 if (retval) 295 return retval; 296 297 mtd->bitflip_threshold = bitflip_threshold; 298 return count; 299 } 300 MTD_DEVICE_ATTR_RW(bitflip_threshold); 301 302 static ssize_t mtd_ecc_step_size_show(struct device *dev, 303 struct device_attribute *attr, char *buf) 304 { 305 struct mtd_info *mtd = dev_get_drvdata(dev); 306 307 return sysfs_emit(buf, "%u\n", mtd->ecc_step_size); 308 309 } 310 MTD_DEVICE_ATTR_RO(ecc_step_size); 311 312 static ssize_t mtd_corrected_bits_show(struct device *dev, 313 struct device_attribute *attr, char *buf) 314 { 315 struct mtd_info *mtd = dev_get_drvdata(dev); 316 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 317 318 return sysfs_emit(buf, "%u\n", ecc_stats->corrected); 319 } 320 MTD_DEVICE_ATTR_RO(corrected_bits); /* ecc stats corrected */ 321 322 static ssize_t mtd_ecc_failures_show(struct device *dev, 323 struct device_attribute *attr, char *buf) 324 { 325 struct mtd_info *mtd = dev_get_drvdata(dev); 326 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 327 328 return sysfs_emit(buf, "%u\n", ecc_stats->failed); 329 } 330 MTD_DEVICE_ATTR_RO(ecc_failures); /* ecc stats errors */ 331 332 static ssize_t mtd_bad_blocks_show(struct device *dev, 333 struct device_attribute *attr, char *buf) 334 { 335 struct mtd_info *mtd = dev_get_drvdata(dev); 336 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 337 338 return sysfs_emit(buf, "%u\n", ecc_stats->badblocks); 339 } 340 MTD_DEVICE_ATTR_RO(bad_blocks); 341 342 static ssize_t mtd_bbt_blocks_show(struct device *dev, 343 struct device_attribute *attr, char *buf) 344 { 345 struct mtd_info *mtd = dev_get_drvdata(dev); 346 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 347 348 return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks); 349 } 350 MTD_DEVICE_ATTR_RO(bbt_blocks); 351 352 static struct attribute *mtd_attrs[] = { 353 &dev_attr_type.attr, 354 &dev_attr_flags.attr, 355 &dev_attr_size.attr, 356 &dev_attr_erasesize.attr, 357 &dev_attr_writesize.attr, 358 &dev_attr_subpagesize.attr, 359 &dev_attr_oobsize.attr, 360 &dev_attr_oobavail.attr, 361 &dev_attr_numeraseregions.attr, 362 &dev_attr_name.attr, 363 &dev_attr_ecc_strength.attr, 364 &dev_attr_ecc_step_size.attr, 365 &dev_attr_corrected_bits.attr, 366 &dev_attr_ecc_failures.attr, 367 &dev_attr_bad_blocks.attr, 368 &dev_attr_bbt_blocks.attr, 369 &dev_attr_bitflip_threshold.attr, 370 NULL, 371 }; 372 ATTRIBUTE_GROUPS(mtd); 373 374 static const struct device_type mtd_devtype = { 375 .name = "mtd", 376 .groups = mtd_groups, 377 .release = mtd_release, 378 }; 379 380 static bool mtd_expert_analysis_mode; 381 382 #ifdef CONFIG_DEBUG_FS 383 bool mtd_check_expert_analysis_mode(void) 384 { 385 const char *mtd_expert_analysis_warning = 386 "Bad block checks have been entirely disabled.\n" 387 "This is only reserved for post-mortem forensics and debug purposes.\n" 388 "Never enable this mode if you do not know what you are doing!\n"; 389 390 return WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning); 391 } 392 EXPORT_SYMBOL_GPL(mtd_check_expert_analysis_mode); 393 #endif 394 395 static struct dentry *dfs_dir_mtd; 396 397 static int mtd_ooblayout_show(struct seq_file *s, void *p, 398 int (*iter)(struct mtd_info *, int section, 399 struct mtd_oob_region *region)) 400 { 401 struct mtd_info *mtd = s->private; 402 int section; 403 404 for (section = 0;; section++) { 405 struct mtd_oob_region region; 406 int err; 407 408 err = iter(mtd, section, ®ion); 409 if (err) { 410 if (err == -ERANGE) 411 break; 412 413 return err; 414 } 415 416 seq_printf(s, "%-3d %4u %4u\n", section, region.offset, 417 region.length); 418 } 419 420 return 0; 421 } 422 423 static int mtd_ooblayout_ecc_show(struct seq_file *s, void *p) 424 { 425 return mtd_ooblayout_show(s, p, mtd_ooblayout_ecc); 426 } 427 DEFINE_SHOW_ATTRIBUTE(mtd_ooblayout_ecc); 428 429 static int mtd_ooblayout_free_show(struct seq_file *s, void *p) 430 { 431 return mtd_ooblayout_show(s, p, mtd_ooblayout_free); 432 } 433 DEFINE_SHOW_ATTRIBUTE(mtd_ooblayout_free); 434 435 static void mtd_debugfs_populate(struct mtd_info *mtd) 436 { 437 struct device *dev = &mtd->dev; 438 struct mtd_oob_region region; 439 440 if (IS_ERR_OR_NULL(dfs_dir_mtd)) 441 return; 442 443 mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(dev), dfs_dir_mtd); 444 if (IS_ERR_OR_NULL(mtd->dbg.dfs_dir)) 445 return; 446 447 /* Create ooblayout files only if at least one region is present. */ 448 if (mtd_ooblayout_ecc(mtd, 0, ®ion) == 0) 449 debugfs_create_file("ooblayout_ecc", 0444, mtd->dbg.dfs_dir, 450 mtd, &mtd_ooblayout_ecc_fops); 451 452 if (mtd_ooblayout_free(mtd, 0, ®ion) == 0) 453 debugfs_create_file("ooblayout_free", 0444, mtd->dbg.dfs_dir, 454 mtd, &mtd_ooblayout_free_fops); 455 } 456 457 #ifndef CONFIG_MMU 458 unsigned mtd_mmap_capabilities(struct mtd_info *mtd) 459 { 460 switch (mtd->type) { 461 case MTD_RAM: 462 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | 463 NOMMU_MAP_READ | NOMMU_MAP_WRITE; 464 case MTD_ROM: 465 return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC | 466 NOMMU_MAP_READ; 467 default: 468 return NOMMU_MAP_COPY; 469 } 470 } 471 EXPORT_SYMBOL_GPL(mtd_mmap_capabilities); 472 #endif 473 474 static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state, 475 void *cmd) 476 { 477 struct mtd_info *mtd; 478 479 mtd = container_of(n, struct mtd_info, reboot_notifier); 480 mtd->_reboot(mtd); 481 482 return NOTIFY_DONE; 483 } 484 485 /** 486 * mtd_wunit_to_pairing_info - get pairing information of a wunit 487 * @mtd: pointer to new MTD device info structure 488 * @wunit: write unit we are interested in 489 * @info: returned pairing information 490 * 491 * Retrieve pairing information associated to the wunit. 492 * This is mainly useful when dealing with MLC/TLC NANDs where pages can be 493 * paired together, and where programming a page may influence the page it is 494 * paired with. 495 * The notion of page is replaced by the term wunit (write-unit) to stay 496 * consistent with the ->writesize field. 497 * 498 * The @wunit argument can be extracted from an absolute offset using 499 * mtd_offset_to_wunit(). @info is filled with the pairing information attached 500 * to @wunit. 501 * 502 * From the pairing info the MTD user can find all the wunits paired with 503 * @wunit using the following loop: 504 * 505 * for (i = 0; i < mtd_pairing_groups(mtd); i++) { 506 * info.pair = i; 507 * mtd_pairing_info_to_wunit(mtd, &info); 508 * ... 509 * } 510 */ 511 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit, 512 struct mtd_pairing_info *info) 513 { 514 struct mtd_info *master = mtd_get_master(mtd); 515 int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master); 516 517 if (wunit < 0 || wunit >= npairs) 518 return -EINVAL; 519 520 if (master->pairing && master->pairing->get_info) 521 return master->pairing->get_info(master, wunit, info); 522 523 info->group = 0; 524 info->pair = wunit; 525 526 return 0; 527 } 528 EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info); 529 530 /** 531 * mtd_pairing_info_to_wunit - get wunit from pairing information 532 * @mtd: pointer to new MTD device info structure 533 * @info: pairing information struct 534 * 535 * Returns a positive number representing the wunit associated to the info 536 * struct, or a negative error code. 537 * 538 * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to 539 * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info() 540 * doc). 541 * 542 * It can also be used to only program the first page of each pair (i.e. 543 * page attached to group 0), which allows one to use an MLC NAND in 544 * software-emulated SLC mode: 545 * 546 * info.group = 0; 547 * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd); 548 * for (info.pair = 0; info.pair < npairs; info.pair++) { 549 * wunit = mtd_pairing_info_to_wunit(mtd, &info); 550 * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit), 551 * mtd->writesize, &retlen, buf + (i * mtd->writesize)); 552 * } 553 */ 554 int mtd_pairing_info_to_wunit(struct mtd_info *mtd, 555 const struct mtd_pairing_info *info) 556 { 557 struct mtd_info *master = mtd_get_master(mtd); 558 int ngroups = mtd_pairing_groups(master); 559 int npairs = mtd_wunit_per_eb(master) / ngroups; 560 561 if (!info || info->pair < 0 || info->pair >= npairs || 562 info->group < 0 || info->group >= ngroups) 563 return -EINVAL; 564 565 if (master->pairing && master->pairing->get_wunit) 566 return mtd->pairing->get_wunit(master, info); 567 568 return info->pair; 569 } 570 EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit); 571 572 /** 573 * mtd_pairing_groups - get the number of pairing groups 574 * @mtd: pointer to new MTD device info structure 575 * 576 * Returns the number of pairing groups. 577 * 578 * This number is usually equal to the number of bits exposed by a single 579 * cell, and can be used in conjunction with mtd_pairing_info_to_wunit() 580 * to iterate over all pages of a given pair. 581 */ 582 int mtd_pairing_groups(struct mtd_info *mtd) 583 { 584 struct mtd_info *master = mtd_get_master(mtd); 585 586 if (!master->pairing || !master->pairing->ngroups) 587 return 1; 588 589 return master->pairing->ngroups; 590 } 591 EXPORT_SYMBOL_GPL(mtd_pairing_groups); 592 593 static int mtd_nvmem_reg_read(void *priv, unsigned int offset, 594 void *val, size_t bytes) 595 { 596 struct mtd_info *mtd = priv; 597 size_t retlen; 598 int err; 599 600 err = mtd_read(mtd, offset, bytes, &retlen, val); 601 if (err && err != -EUCLEAN) 602 return err; 603 604 return retlen == bytes ? 0 : -EIO; 605 } 606 607 static int mtd_nvmem_add(struct mtd_info *mtd) 608 { 609 struct device_node *node = mtd_get_of_node(mtd); 610 struct nvmem_config config = {}; 611 612 config.id = NVMEM_DEVID_NONE; 613 config.dev = &mtd->dev; 614 config.name = dev_name(&mtd->dev); 615 config.owner = THIS_MODULE; 616 config.add_legacy_fixed_of_cells = of_device_is_compatible(node, "nvmem-cells"); 617 config.reg_read = mtd_nvmem_reg_read; 618 config.size = mtd->size; 619 config.word_size = 1; 620 config.stride = 1; 621 config.read_only = true; 622 config.root_only = true; 623 config.ignore_wp = true; 624 config.priv = mtd; 625 626 mtd->nvmem = nvmem_register(&config); 627 if (IS_ERR(mtd->nvmem)) { 628 /* Just ignore if there is no NVMEM support in the kernel */ 629 if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) 630 mtd->nvmem = NULL; 631 else 632 return dev_err_probe(&mtd->dev, PTR_ERR(mtd->nvmem), 633 "Failed to register NVMEM device\n"); 634 } 635 636 return 0; 637 } 638 639 static void mtd_check_of_node(struct mtd_info *mtd) 640 { 641 struct device_node *partitions, *parent_dn, *mtd_dn = NULL; 642 const char *pname, *prefix = "partition-"; 643 int plen, mtd_name_len, offset, prefix_len; 644 645 /* Check if MTD already has a device node */ 646 if (mtd_get_of_node(mtd)) 647 return; 648 649 if (!mtd_is_partition(mtd)) 650 return; 651 652 parent_dn = of_node_get(mtd_get_of_node(mtd->parent)); 653 if (!parent_dn) 654 return; 655 656 if (mtd_is_partition(mtd->parent)) 657 partitions = of_node_get(parent_dn); 658 else 659 partitions = of_get_child_by_name(parent_dn, "partitions"); 660 if (!partitions) 661 goto exit_parent; 662 663 prefix_len = strlen(prefix); 664 mtd_name_len = strlen(mtd->name); 665 666 /* Search if a partition is defined with the same name */ 667 for_each_child_of_node(partitions, mtd_dn) { 668 /* Skip partition with no/wrong prefix */ 669 if (!of_node_name_prefix(mtd_dn, prefix)) 670 continue; 671 672 /* Label have priority. Check that first */ 673 if (!of_property_read_string(mtd_dn, "label", &pname)) { 674 offset = 0; 675 } else { 676 pname = mtd_dn->name; 677 offset = prefix_len; 678 } 679 680 plen = strlen(pname) - offset; 681 if (plen == mtd_name_len && 682 !strncmp(mtd->name, pname + offset, plen)) { 683 mtd_set_of_node(mtd, mtd_dn); 684 of_node_put(mtd_dn); 685 break; 686 } 687 } 688 689 of_node_put(partitions); 690 exit_parent: 691 of_node_put(parent_dn); 692 } 693 694 /** 695 * add_mtd_device - register an MTD device 696 * @mtd: pointer to new MTD device info structure 697 * 698 * Add a device to the list of MTD devices present in the system, and 699 * notify each currently active MTD 'user' of its arrival. Returns 700 * zero on success or non-zero on failure. 701 */ 702 703 int add_mtd_device(struct mtd_info *mtd) 704 { 705 struct device_node *np = mtd_get_of_node(mtd); 706 struct mtd_info *master = mtd_get_master(mtd); 707 struct mtd_notifier *not; 708 int i, error, ofidx; 709 710 /* 711 * May occur, for instance, on buggy drivers which call 712 * mtd_device_parse_register() multiple times on the same master MTD, 713 * especially with CONFIG_MTD_PARTITIONED_MASTER=y. 714 */ 715 if (WARN_ONCE(mtd->dev.type, "MTD already registered\n")) 716 return -EEXIST; 717 718 BUG_ON(mtd->writesize == 0); 719 720 /* 721 * MTD drivers should implement ->_{write,read}() or 722 * ->_{write,read}_oob(), but not both. 723 */ 724 if (WARN_ON((mtd->_write && mtd->_write_oob) || 725 (mtd->_read && mtd->_read_oob))) 726 return -EINVAL; 727 728 if (WARN_ON((!mtd->erasesize || !master->_erase) && 729 !(mtd->flags & MTD_NO_ERASE))) 730 return -EINVAL; 731 732 /* 733 * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the 734 * master is an MLC NAND and has a proper pairing scheme defined. 735 * We also reject masters that implement ->_writev() for now, because 736 * NAND controller drivers don't implement this hook, and adding the 737 * SLC -> MLC address/length conversion to this path is useless if we 738 * don't have a user. 739 */ 740 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION && 741 (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH || 742 !master->pairing || master->_writev)) 743 return -EINVAL; 744 745 mutex_lock(&mtd_table_mutex); 746 747 ofidx = -1; 748 if (np) 749 ofidx = of_alias_get_id(np, "mtd"); 750 if (ofidx >= 0) 751 i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL); 752 else 753 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); 754 if (i < 0) { 755 error = i; 756 goto fail_locked; 757 } 758 759 mtd->index = i; 760 kref_init(&mtd->refcnt); 761 762 /* default value if not set by driver */ 763 if (mtd->bitflip_threshold == 0) 764 mtd->bitflip_threshold = mtd->ecc_strength; 765 766 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 767 int ngroups = mtd_pairing_groups(master); 768 769 mtd->erasesize /= ngroups; 770 mtd->size = (u64)mtd_div_by_eb(mtd->size, master) * 771 mtd->erasesize; 772 } 773 774 if (is_power_of_2(mtd->erasesize)) 775 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; 776 else 777 mtd->erasesize_shift = 0; 778 779 if (is_power_of_2(mtd->writesize)) 780 mtd->writesize_shift = ffs(mtd->writesize) - 1; 781 else 782 mtd->writesize_shift = 0; 783 784 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; 785 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; 786 787 /* Some chips always power up locked. Unlock them now */ 788 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { 789 error = mtd_unlock(mtd, 0, mtd->size); 790 if (error && error != -EOPNOTSUPP) 791 printk(KERN_WARNING 792 "%s: unlock failed, writes may not work\n", 793 mtd->name); 794 /* Ignore unlock failures? */ 795 error = 0; 796 } 797 798 /* Caller should have set dev.parent to match the 799 * physical device, if appropriate. 800 */ 801 mtd->dev.type = &mtd_devtype; 802 mtd->dev.class = &mtd_class; 803 mtd->dev.devt = MTD_DEVT(i); 804 error = dev_set_name(&mtd->dev, "mtd%d", i); 805 if (error) 806 goto fail_devname; 807 dev_set_drvdata(&mtd->dev, mtd); 808 mtd_check_of_node(mtd); 809 of_node_get(mtd_get_of_node(mtd)); 810 error = device_register(&mtd->dev); 811 if (error) 812 goto fail_added; 813 814 /* Add the nvmem provider */ 815 error = mtd_nvmem_add(mtd); 816 if (error) 817 goto fail_nvmem_add; 818 819 mtd_debugfs_populate(mtd); 820 821 device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL, 822 "mtd%dro", i); 823 824 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 825 /* No need to get a refcount on the module containing 826 the notifier, since we hold the mtd_table_mutex */ 827 list_for_each_entry(not, &mtd_notifiers, list) 828 not->add(mtd); 829 830 mutex_unlock(&mtd_table_mutex); 831 832 if (of_property_read_bool(mtd_get_of_node(mtd), "linux,rootfs")) { 833 if (IS_BUILTIN(CONFIG_MTD)) { 834 pr_info("mtd: setting mtd%d (%s) as root device\n", mtd->index, mtd->name); 835 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, mtd->index); 836 } else { 837 pr_warn("mtd: can't set mtd%d (%s) as root device - mtd must be builtin\n", 838 mtd->index, mtd->name); 839 } 840 } 841 842 /* We _know_ we aren't being removed, because 843 our caller is still holding us here. So none 844 of this try_ nonsense, and no bitching about it 845 either. :) */ 846 __module_get(THIS_MODULE); 847 return 0; 848 849 fail_nvmem_add: 850 device_del(&mtd->dev); 851 fail_added: 852 /* 853 * Clear type and set nop release to prevent mtd_release() -> 854 * release_mtd_partition() -> free_partition() from freeing mtd. 855 * The caller handles cleanup on failure. 856 */ 857 mtd->dev.type = NULL; 858 mtd->dev.release = mtd_dev_release_nop; 859 put_device(&mtd->dev); 860 of_node_put(mtd_get_of_node(mtd)); 861 fail_devname: 862 idr_remove(&mtd_idr, i); 863 fail_locked: 864 mutex_unlock(&mtd_table_mutex); 865 return error; 866 } 867 868 /** 869 * del_mtd_device - unregister an MTD device 870 * @mtd: pointer to MTD device info structure 871 * 872 * Remove a device from the list of MTD devices present in the system, 873 * and notify each currently active MTD 'user' of its departure. 874 * Returns zero on success or 1 on failure, which currently will happen 875 * if the requested device does not appear to be present in the list. 876 */ 877 878 int del_mtd_device(struct mtd_info *mtd) 879 { 880 int ret; 881 struct mtd_notifier *not; 882 883 mutex_lock(&mtd_table_mutex); 884 885 if (idr_find(&mtd_idr, mtd->index) != mtd) { 886 ret = -ENODEV; 887 goto out_error; 888 } 889 890 /* No need to get a refcount on the module containing 891 the notifier, since we hold the mtd_table_mutex */ 892 list_for_each_entry(not, &mtd_notifiers, list) 893 not->remove(mtd); 894 895 kref_put(&mtd->refcnt, mtd_device_release); 896 ret = 0; 897 898 out_error: 899 mutex_unlock(&mtd_table_mutex); 900 return ret; 901 } 902 903 /* 904 * Set a few defaults based on the parent devices, if not provided by the 905 * driver 906 */ 907 static void mtd_set_dev_defaults(struct mtd_info *mtd) 908 { 909 if (mtd->dev.parent) { 910 if (!mtd->owner && mtd->dev.parent->driver) 911 mtd->owner = mtd->dev.parent->driver->owner; 912 if (!mtd->name) 913 mtd->name = dev_name(mtd->dev.parent); 914 } else { 915 pr_debug("mtd device won't show a device symlink in sysfs\n"); 916 } 917 918 INIT_LIST_HEAD(&mtd->partitions); 919 mutex_init(&mtd->master.partitions_lock); 920 mutex_init(&mtd->master.chrdev_lock); 921 } 922 923 static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user) 924 { 925 struct otp_info *info; 926 ssize_t size = 0; 927 unsigned int i; 928 size_t retlen; 929 int ret; 930 931 info = kmalloc(PAGE_SIZE, GFP_KERNEL); 932 if (!info) 933 return -ENOMEM; 934 935 if (is_user) 936 ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info); 937 else 938 ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info); 939 if (ret) 940 goto err; 941 942 for (i = 0; i < retlen / sizeof(*info); i++) 943 size += info[i].length; 944 945 kfree(info); 946 return size; 947 948 err: 949 kfree(info); 950 951 /* ENODATA means there is no OTP region. */ 952 return ret == -ENODATA ? 0 : ret; 953 } 954 955 static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd, 956 const char *compatible, 957 int size, 958 nvmem_reg_read_t reg_read) 959 { 960 struct nvmem_device *nvmem = NULL; 961 struct nvmem_config config = {}; 962 struct device_node *np; 963 964 /* DT binding is optional */ 965 np = of_get_compatible_child(mtd->dev.of_node, compatible); 966 967 /* OTP nvmem will be registered on the physical device */ 968 config.dev = mtd->dev.parent; 969 config.name = compatible; 970 config.id = NVMEM_DEVID_AUTO; 971 config.owner = THIS_MODULE; 972 config.add_legacy_fixed_of_cells = !mtd_type_is_nand(mtd); 973 config.type = NVMEM_TYPE_OTP; 974 config.root_only = true; 975 config.ignore_wp = true; 976 config.reg_read = reg_read; 977 config.size = size; 978 config.of_node = np; 979 config.priv = mtd; 980 981 nvmem = nvmem_register(&config); 982 /* Just ignore if there is no NVMEM support in the kernel */ 983 if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EOPNOTSUPP) 984 nvmem = NULL; 985 986 of_node_put(np); 987 988 return nvmem; 989 } 990 991 static int mtd_nvmem_user_otp_reg_read(void *priv, unsigned int offset, 992 void *val, size_t bytes) 993 { 994 struct mtd_info *mtd = priv; 995 size_t retlen; 996 int ret; 997 998 ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val); 999 if (ret) 1000 return ret; 1001 1002 return retlen == bytes ? 0 : -EIO; 1003 } 1004 1005 static int mtd_nvmem_fact_otp_reg_read(void *priv, unsigned int offset, 1006 void *val, size_t bytes) 1007 { 1008 struct mtd_info *mtd = priv; 1009 size_t retlen; 1010 int ret; 1011 1012 ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val); 1013 if (ret) 1014 return ret; 1015 1016 return retlen == bytes ? 0 : -EIO; 1017 } 1018 1019 static int mtd_otp_nvmem_add(struct mtd_info *mtd) 1020 { 1021 struct device *dev = mtd->dev.parent; 1022 struct nvmem_device *nvmem; 1023 ssize_t size; 1024 int err; 1025 1026 if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) { 1027 size = mtd_otp_size(mtd, true); 1028 if (size < 0) { 1029 err = size; 1030 goto err; 1031 } 1032 1033 if (size > 0) { 1034 nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size, 1035 mtd_nvmem_user_otp_reg_read); 1036 if (IS_ERR(nvmem)) { 1037 err = PTR_ERR(nvmem); 1038 goto err; 1039 } 1040 mtd->otp_user_nvmem = nvmem; 1041 } 1042 } 1043 1044 if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) { 1045 size = mtd_otp_size(mtd, false); 1046 if (size < 0) { 1047 err = size; 1048 goto err; 1049 } 1050 1051 if (size > 0) { 1052 /* 1053 * The factory OTP contains thing such as a unique serial 1054 * number and is small, so let's read it out and put it 1055 * into the entropy pool. 1056 */ 1057 void *otp; 1058 1059 otp = kmalloc(size, GFP_KERNEL); 1060 if (!otp) { 1061 err = -ENOMEM; 1062 goto err; 1063 } 1064 err = mtd_nvmem_fact_otp_reg_read(mtd, 0, otp, size); 1065 if (err < 0) { 1066 kfree(otp); 1067 goto err; 1068 } 1069 add_device_randomness(otp, err); 1070 kfree(otp); 1071 1072 nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size, 1073 mtd_nvmem_fact_otp_reg_read); 1074 if (IS_ERR(nvmem)) { 1075 err = PTR_ERR(nvmem); 1076 goto err; 1077 } 1078 mtd->otp_factory_nvmem = nvmem; 1079 } 1080 } 1081 1082 return 0; 1083 1084 err: 1085 nvmem_unregister(mtd->otp_user_nvmem); 1086 mtd->otp_user_nvmem = NULL; 1087 /* Don't report error if OTP is not supported. */ 1088 if (err == -EOPNOTSUPP) 1089 return 0; 1090 return dev_err_probe(dev, err, "Failed to register OTP NVMEM device\n"); 1091 } 1092 1093 /** 1094 * mtd_device_parse_register - parse partitions and register an MTD device. 1095 * 1096 * @mtd: the MTD device to register 1097 * @types: the list of MTD partition probes to try, see 1098 * 'parse_mtd_partitions()' for more information 1099 * @parser_data: MTD partition parser-specific data 1100 * @parts: fallback partition information to register, if parsing fails; 1101 * only valid if %nr_parts > %0 1102 * @nr_parts: the number of partitions in parts, if zero then the full 1103 * MTD device is registered if no partition info is found 1104 * 1105 * This function aggregates MTD partitions parsing (done by 1106 * 'parse_mtd_partitions()') and MTD device and partitions registering. It 1107 * basically follows the most common pattern found in many MTD drivers: 1108 * 1109 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is 1110 * registered first. 1111 * * Then It tries to probe partitions on MTD device @mtd using parsers 1112 * specified in @types (if @types is %NULL, then the default list of parsers 1113 * is used, see 'parse_mtd_partitions()' for more information). If none are 1114 * found this functions tries to fallback to information specified in 1115 * @parts/@nr_parts. 1116 * * If no partitions were found this function just registers the MTD device 1117 * @mtd and exits. 1118 * 1119 * Returns zero in case of success and a negative error code in case of failure. 1120 */ 1121 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, 1122 struct mtd_part_parser_data *parser_data, 1123 const struct mtd_partition *parts, 1124 int nr_parts) 1125 { 1126 int ret, err; 1127 1128 mtd_set_dev_defaults(mtd); 1129 1130 ret = mtd_otp_nvmem_add(mtd); 1131 if (ret) 1132 goto out; 1133 1134 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { 1135 ret = add_mtd_device(mtd); 1136 if (ret) 1137 goto out; 1138 } 1139 1140 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1141 ret = mtd_virt_concat_node_create(); 1142 if (ret < 0) 1143 goto out; 1144 } 1145 1146 /* Prefer parsed partitions over driver-provided fallback */ 1147 ret = parse_mtd_partitions(mtd, types, parser_data); 1148 if (ret == -EPROBE_DEFER) 1149 goto out; 1150 1151 if (ret > 0) 1152 ret = 0; 1153 else if (nr_parts) 1154 ret = add_mtd_partitions(mtd, parts, nr_parts); 1155 else if (!device_is_registered(&mtd->dev)) 1156 ret = add_mtd_device(mtd); 1157 else 1158 ret = 0; 1159 1160 if (ret) 1161 goto out; 1162 1163 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1164 ret = mtd_virt_concat_create_join(); 1165 if (ret < 0) 1166 goto out; 1167 } 1168 /* 1169 * FIXME: some drivers unfortunately call this function more than once. 1170 * So we have to check if we've already assigned the reboot notifier. 1171 * 1172 * Generally, we can make multiple calls work for most cases, but it 1173 * does cause problems with parse_mtd_partitions() above (e.g., 1174 * cmdlineparts will register partitions more than once). 1175 */ 1176 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, 1177 "MTD already registered\n"); 1178 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { 1179 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; 1180 register_reboot_notifier(&mtd->reboot_notifier); 1181 } 1182 1183 out: 1184 if (ret) { 1185 nvmem_unregister(mtd->otp_user_nvmem); 1186 nvmem_unregister(mtd->otp_factory_nvmem); 1187 } 1188 1189 if (ret && device_is_registered(&mtd->dev)) { 1190 err = del_mtd_device(mtd); 1191 if (err) 1192 pr_err("Error when deleting MTD device (%d)\n", err); 1193 } 1194 1195 return ret; 1196 } 1197 EXPORT_SYMBOL_GPL(mtd_device_parse_register); 1198 1199 /** 1200 * mtd_device_unregister - unregister an existing MTD device. 1201 * 1202 * @master: the MTD device to unregister. This will unregister both the master 1203 * and any partitions if registered. 1204 */ 1205 int mtd_device_unregister(struct mtd_info *master) 1206 { 1207 int err; 1208 1209 if (master->_reboot) { 1210 unregister_reboot_notifier(&master->reboot_notifier); 1211 memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier)); 1212 } 1213 1214 nvmem_unregister(master->otp_user_nvmem); 1215 nvmem_unregister(master->otp_factory_nvmem); 1216 1217 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1218 err = mtd_virt_concat_destroy(master); 1219 if (err) 1220 return err; 1221 } 1222 err = del_mtd_partitions(master); 1223 if (err) 1224 return err; 1225 1226 if (!device_is_registered(&master->dev)) 1227 return 0; 1228 1229 return del_mtd_device(master); 1230 } 1231 EXPORT_SYMBOL_GPL(mtd_device_unregister); 1232 1233 /** 1234 * register_mtd_user - register a 'user' of MTD devices. 1235 * @new: pointer to notifier info structure 1236 * 1237 * Registers a pair of callbacks function to be called upon addition 1238 * or removal of MTD devices. Causes the 'add' callback to be immediately 1239 * invoked for each MTD device currently present in the system. 1240 */ 1241 void register_mtd_user (struct mtd_notifier *new) 1242 { 1243 struct mtd_info *mtd; 1244 1245 mutex_lock(&mtd_table_mutex); 1246 1247 list_add(&new->list, &mtd_notifiers); 1248 1249 __module_get(THIS_MODULE); 1250 1251 mtd_for_each_device(mtd) 1252 new->add(mtd); 1253 1254 mutex_unlock(&mtd_table_mutex); 1255 } 1256 EXPORT_SYMBOL_GPL(register_mtd_user); 1257 1258 /** 1259 * unregister_mtd_user - unregister a 'user' of MTD devices. 1260 * @old: pointer to notifier info structure 1261 * 1262 * Removes a callback function pair from the list of 'users' to be 1263 * notified upon addition or removal of MTD devices. Causes the 1264 * 'remove' callback to be immediately invoked for each MTD device 1265 * currently present in the system. 1266 */ 1267 int unregister_mtd_user (struct mtd_notifier *old) 1268 { 1269 struct mtd_info *mtd; 1270 1271 mutex_lock(&mtd_table_mutex); 1272 1273 module_put(THIS_MODULE); 1274 1275 mtd_for_each_device(mtd) 1276 old->remove(mtd); 1277 1278 list_del(&old->list); 1279 mutex_unlock(&mtd_table_mutex); 1280 return 0; 1281 } 1282 EXPORT_SYMBOL_GPL(unregister_mtd_user); 1283 1284 /** 1285 * get_mtd_device - obtain a validated handle for an MTD device 1286 * @mtd: last known address of the required MTD device 1287 * @num: internal device number of the required MTD device 1288 * 1289 * Given a number and NULL address, return the num'th entry in the device 1290 * table, if any. Given an address and num == -1, search the device table 1291 * for a device with that address and return if it's still present. Given 1292 * both, return the num'th driver only if its address matches. Return 1293 * error code if not. 1294 */ 1295 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 1296 { 1297 struct mtd_info *ret = NULL, *other; 1298 int err = -ENODEV; 1299 1300 mutex_lock(&mtd_table_mutex); 1301 1302 if (num == -1) { 1303 mtd_for_each_device(other) { 1304 if (other == mtd) { 1305 ret = mtd; 1306 break; 1307 } 1308 } 1309 } else if (num >= 0) { 1310 ret = idr_find(&mtd_idr, num); 1311 if (mtd && mtd != ret) 1312 ret = NULL; 1313 } 1314 1315 if (!ret) { 1316 ret = ERR_PTR(err); 1317 goto out; 1318 } 1319 1320 err = __get_mtd_device(ret); 1321 if (err) 1322 ret = ERR_PTR(err); 1323 out: 1324 mutex_unlock(&mtd_table_mutex); 1325 return ret; 1326 } 1327 EXPORT_SYMBOL_GPL(get_mtd_device); 1328 1329 1330 int __get_mtd_device(struct mtd_info *mtd) 1331 { 1332 struct mtd_info *master = mtd_get_master(mtd); 1333 int err; 1334 1335 if (master->_get_device) { 1336 err = master->_get_device(master); 1337 if (err) 1338 return err; 1339 } 1340 1341 if (!try_module_get(master->owner)) { 1342 if (master->_put_device) 1343 master->_put_device(master); 1344 return -ENODEV; 1345 } 1346 1347 while (mtd) { 1348 if (mtd != master) 1349 kref_get(&mtd->refcnt); 1350 mtd = mtd->parent; 1351 } 1352 1353 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) 1354 kref_get(&master->refcnt); 1355 1356 return 0; 1357 } 1358 EXPORT_SYMBOL_GPL(__get_mtd_device); 1359 1360 /** 1361 * of_get_mtd_device_by_node - obtain an MTD device associated with a given node 1362 * 1363 * @np: device tree node 1364 */ 1365 struct mtd_info *of_get_mtd_device_by_node(struct device_node *np) 1366 { 1367 struct mtd_info *mtd = NULL; 1368 struct mtd_info *tmp; 1369 int err; 1370 1371 mutex_lock(&mtd_table_mutex); 1372 1373 err = -EPROBE_DEFER; 1374 mtd_for_each_device(tmp) { 1375 if (mtd_get_of_node(tmp) == np) { 1376 mtd = tmp; 1377 err = __get_mtd_device(mtd); 1378 break; 1379 } 1380 } 1381 1382 mutex_unlock(&mtd_table_mutex); 1383 1384 return err ? ERR_PTR(err) : mtd; 1385 } 1386 EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node); 1387 1388 /** 1389 * get_mtd_device_nm - obtain a validated handle for an MTD device by 1390 * device name 1391 * @name: MTD device name to open 1392 * 1393 * This function returns MTD device description structure in case of 1394 * success and an error code in case of failure. 1395 */ 1396 struct mtd_info *get_mtd_device_nm(const char *name) 1397 { 1398 int err = -ENODEV; 1399 struct mtd_info *mtd = NULL, *other; 1400 1401 mutex_lock(&mtd_table_mutex); 1402 1403 mtd_for_each_device(other) { 1404 if (!strcmp(name, other->name)) { 1405 mtd = other; 1406 break; 1407 } 1408 } 1409 1410 if (!mtd) 1411 goto out_unlock; 1412 1413 err = __get_mtd_device(mtd); 1414 if (err) 1415 goto out_unlock; 1416 1417 mutex_unlock(&mtd_table_mutex); 1418 return mtd; 1419 1420 out_unlock: 1421 mutex_unlock(&mtd_table_mutex); 1422 return ERR_PTR(err); 1423 } 1424 EXPORT_SYMBOL_GPL(get_mtd_device_nm); 1425 1426 void put_mtd_device(struct mtd_info *mtd) 1427 { 1428 mutex_lock(&mtd_table_mutex); 1429 __put_mtd_device(mtd); 1430 mutex_unlock(&mtd_table_mutex); 1431 1432 } 1433 EXPORT_SYMBOL_GPL(put_mtd_device); 1434 1435 void __put_mtd_device(struct mtd_info *mtd) 1436 { 1437 struct mtd_info *master = mtd_get_master(mtd); 1438 1439 while (mtd) { 1440 /* kref_put() can relese mtd, so keep a reference mtd->parent */ 1441 struct mtd_info *parent = mtd->parent; 1442 1443 if (mtd != master) 1444 kref_put(&mtd->refcnt, mtd_device_release); 1445 mtd = parent; 1446 } 1447 1448 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) 1449 kref_put(&master->refcnt, mtd_device_release); 1450 1451 module_put(master->owner); 1452 1453 /* must be the last as master can be freed in the _put_device */ 1454 if (master->_put_device) 1455 master->_put_device(master); 1456 } 1457 EXPORT_SYMBOL_GPL(__put_mtd_device); 1458 1459 /* 1460 * Erase is an synchronous operation. Device drivers are epected to return a 1461 * negative error code if the operation failed and update instr->fail_addr 1462 * to point the portion that was not properly erased. 1463 */ 1464 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 1465 { 1466 struct mtd_info *master = mtd_get_master(mtd); 1467 u64 mst_ofs = mtd_get_master_ofs(mtd, 0); 1468 struct erase_info adjinstr; 1469 int ret; 1470 1471 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 1472 adjinstr = *instr; 1473 1474 if (!mtd->erasesize || !master->_erase) 1475 return -ENOTSUPP; 1476 1477 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) 1478 return -EINVAL; 1479 if (!(mtd->flags & MTD_WRITEABLE)) 1480 return -EROFS; 1481 1482 if (!instr->len) 1483 return 0; 1484 1485 ledtrig_mtd_activity(); 1486 1487 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 1488 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * 1489 master->erasesize; 1490 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * 1491 master->erasesize) - 1492 adjinstr.addr; 1493 } 1494 1495 adjinstr.addr += mst_ofs; 1496 1497 ret = master->_erase(master, &adjinstr); 1498 1499 if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) { 1500 instr->fail_addr = adjinstr.fail_addr - mst_ofs; 1501 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 1502 instr->fail_addr = mtd_div_by_eb(instr->fail_addr, 1503 master); 1504 instr->fail_addr *= mtd->erasesize; 1505 } 1506 } 1507 1508 return ret; 1509 } 1510 EXPORT_SYMBOL_GPL(mtd_erase); 1511 ALLOW_ERROR_INJECTION(mtd_erase, ERRNO); 1512 1513 /* 1514 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. 1515 */ 1516 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 1517 void **virt, resource_size_t *phys) 1518 { 1519 struct mtd_info *master = mtd_get_master(mtd); 1520 1521 *retlen = 0; 1522 *virt = NULL; 1523 if (phys) 1524 *phys = 0; 1525 if (!master->_point) 1526 return -EOPNOTSUPP; 1527 if (from < 0 || from >= mtd->size || len > mtd->size - from) 1528 return -EINVAL; 1529 if (!len) 1530 return 0; 1531 1532 from = mtd_get_master_ofs(mtd, from); 1533 return master->_point(master, from, len, retlen, virt, phys); 1534 } 1535 EXPORT_SYMBOL_GPL(mtd_point); 1536 1537 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 1538 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 1539 { 1540 struct mtd_info *master = mtd_get_master(mtd); 1541 1542 if (!master->_unpoint) 1543 return -EOPNOTSUPP; 1544 if (from < 0 || from >= mtd->size || len > mtd->size - from) 1545 return -EINVAL; 1546 if (!len) 1547 return 0; 1548 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); 1549 } 1550 EXPORT_SYMBOL_GPL(mtd_unpoint); 1551 1552 /* 1553 * Allow NOMMU mmap() to directly map the device (if not NULL) 1554 * - return the address to which the offset maps 1555 * - return -ENOSYS to indicate refusal to do the mapping 1556 */ 1557 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 1558 unsigned long offset, unsigned long flags) 1559 { 1560 size_t retlen; 1561 void *virt; 1562 int ret; 1563 1564 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); 1565 if (ret) 1566 return ret; 1567 if (retlen != len) { 1568 mtd_unpoint(mtd, offset, retlen); 1569 return -ENOSYS; 1570 } 1571 return (unsigned long)virt; 1572 } 1573 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); 1574 1575 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, 1576 const struct mtd_ecc_stats *old_stats) 1577 { 1578 struct mtd_ecc_stats diff; 1579 1580 if (master == mtd) 1581 return; 1582 1583 diff = master->ecc_stats; 1584 diff.failed -= old_stats->failed; 1585 diff.corrected -= old_stats->corrected; 1586 1587 while (mtd->parent) { 1588 mtd->ecc_stats.failed += diff.failed; 1589 mtd->ecc_stats.corrected += diff.corrected; 1590 mtd = mtd->parent; 1591 } 1592 } 1593 1594 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 1595 u_char *buf) 1596 { 1597 struct mtd_oob_ops ops = { 1598 .len = len, 1599 .datbuf = buf, 1600 }; 1601 int ret; 1602 1603 ret = mtd_read_oob(mtd, from, &ops); 1604 *retlen = ops.retlen; 1605 1606 WARN_ON_ONCE(*retlen != len && mtd_is_bitflip_or_eccerr(ret)); 1607 1608 return ret; 1609 } 1610 EXPORT_SYMBOL_GPL(mtd_read); 1611 ALLOW_ERROR_INJECTION(mtd_read, ERRNO); 1612 1613 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 1614 const u_char *buf) 1615 { 1616 struct mtd_oob_ops ops = { 1617 .len = len, 1618 .datbuf = (u8 *)buf, 1619 }; 1620 int ret; 1621 1622 ret = mtd_write_oob(mtd, to, &ops); 1623 *retlen = ops.retlen; 1624 1625 return ret; 1626 } 1627 EXPORT_SYMBOL_GPL(mtd_write); 1628 ALLOW_ERROR_INJECTION(mtd_write, ERRNO); 1629 1630 /* 1631 * In blackbox flight recorder like scenarios we want to make successful writes 1632 * in interrupt context. panic_write() is only intended to be called when its 1633 * known the kernel is about to panic and we need the write to succeed. Since 1634 * the kernel is not going to be running for much longer, this function can 1635 * break locks and delay to ensure the write succeeds (but not sleep). 1636 */ 1637 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 1638 const u_char *buf) 1639 { 1640 struct mtd_info *master = mtd_get_master(mtd); 1641 1642 *retlen = 0; 1643 if (!master->_panic_write) 1644 return -EOPNOTSUPP; 1645 if (to < 0 || to >= mtd->size || len > mtd->size - to) 1646 return -EINVAL; 1647 if (!(mtd->flags & MTD_WRITEABLE)) 1648 return -EROFS; 1649 if (!len) 1650 return 0; 1651 if (!master->oops_panic_write) 1652 master->oops_panic_write = true; 1653 1654 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, 1655 retlen, buf); 1656 } 1657 EXPORT_SYMBOL_GPL(mtd_panic_write); 1658 1659 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, 1660 struct mtd_oob_ops *ops) 1661 { 1662 /* 1663 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving 1664 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in 1665 * this case. 1666 */ 1667 if (!ops->datbuf) 1668 ops->len = 0; 1669 1670 if (!ops->oobbuf) 1671 ops->ooblen = 0; 1672 1673 if (offs < 0 || offs + ops->len > mtd->size) 1674 return -EINVAL; 1675 1676 if (ops->ooblen) { 1677 size_t maxooblen; 1678 1679 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) 1680 return -EINVAL; 1681 1682 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - 1683 mtd_div_by_ws(offs, mtd)) * 1684 mtd_oobavail(mtd, ops)) - ops->ooboffs; 1685 if (ops->ooblen > maxooblen) 1686 return -EINVAL; 1687 } 1688 1689 return 0; 1690 } 1691 1692 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, 1693 struct mtd_oob_ops *ops) 1694 { 1695 struct mtd_info *master = mtd_get_master(mtd); 1696 int ret; 1697 1698 from = mtd_get_master_ofs(mtd, from); 1699 if (master->_read_oob) 1700 ret = master->_read_oob(master, from, ops); 1701 else 1702 ret = master->_read(master, from, ops->len, &ops->retlen, 1703 ops->datbuf); 1704 1705 return ret; 1706 } 1707 1708 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, 1709 struct mtd_oob_ops *ops) 1710 { 1711 struct mtd_info *master = mtd_get_master(mtd); 1712 int ret; 1713 1714 to = mtd_get_master_ofs(mtd, to); 1715 if (master->_write_oob) 1716 ret = master->_write_oob(master, to, ops); 1717 else 1718 ret = master->_write(master, to, ops->len, &ops->retlen, 1719 ops->datbuf); 1720 1721 return ret; 1722 } 1723 1724 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, 1725 struct mtd_oob_ops *ops) 1726 { 1727 struct mtd_info *master = mtd_get_master(mtd); 1728 int ngroups = mtd_pairing_groups(master); 1729 int npairs = mtd_wunit_per_eb(master) / ngroups; 1730 struct mtd_oob_ops adjops = *ops; 1731 unsigned int wunit, oobavail; 1732 struct mtd_pairing_info info; 1733 int max_bitflips = 0; 1734 u32 ebofs, pageofs; 1735 loff_t base, pos; 1736 1737 ebofs = mtd_mod_by_eb(start, mtd); 1738 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; 1739 info.group = 0; 1740 info.pair = mtd_div_by_ws(ebofs, mtd); 1741 pageofs = mtd_mod_by_ws(ebofs, mtd); 1742 oobavail = mtd_oobavail(mtd, ops); 1743 1744 while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) { 1745 int ret; 1746 1747 if (info.pair >= npairs) { 1748 info.pair = 0; 1749 base += master->erasesize; 1750 } 1751 1752 wunit = mtd_pairing_info_to_wunit(master, &info); 1753 pos = mtd_wunit_to_offset(mtd, base, wunit); 1754 1755 adjops.len = ops->len - ops->retlen; 1756 if (adjops.len > mtd->writesize - pageofs) 1757 adjops.len = mtd->writesize - pageofs; 1758 1759 adjops.ooblen = ops->ooblen - ops->oobretlen; 1760 if (adjops.ooblen > oobavail - adjops.ooboffs) 1761 adjops.ooblen = oobavail - adjops.ooboffs; 1762 1763 if (read) { 1764 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); 1765 if (ret > 0) 1766 max_bitflips = max(max_bitflips, ret); 1767 } else { 1768 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); 1769 } 1770 1771 if (ret < 0) 1772 return ret; 1773 1774 max_bitflips = max(max_bitflips, ret); 1775 ops->retlen += adjops.retlen; 1776 ops->oobretlen += adjops.oobretlen; 1777 adjops.datbuf += adjops.retlen; 1778 adjops.oobbuf += adjops.oobretlen; 1779 adjops.ooboffs = 0; 1780 pageofs = 0; 1781 info.pair++; 1782 } 1783 1784 return max_bitflips; 1785 } 1786 1787 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) 1788 { 1789 struct mtd_info *master = mtd_get_master(mtd); 1790 struct mtd_ecc_stats old_stats = master->ecc_stats; 1791 int ret_code; 1792 1793 ops->retlen = ops->oobretlen = 0; 1794 1795 ret_code = mtd_check_oob_ops(mtd, from, ops); 1796 if (ret_code) 1797 return ret_code; 1798 1799 ledtrig_mtd_activity(); 1800 1801 /* Check the validity of a potential fallback on mtd->_read */ 1802 if (!master->_read_oob && (!master->_read || ops->oobbuf)) 1803 return -EOPNOTSUPP; 1804 1805 if (ops->stats) 1806 memset(ops->stats, 0, sizeof(*ops->stats)); 1807 1808 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 1809 ret_code = mtd_io_emulated_slc(mtd, from, true, ops); 1810 else 1811 ret_code = mtd_read_oob_std(mtd, from, ops); 1812 1813 mtd_update_ecc_stats(mtd, master, &old_stats); 1814 1815 /* 1816 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics 1817 * similar to mtd->_read(), returning a non-negative integer 1818 * representing max bitflips. In other cases, mtd->_read_oob() may 1819 * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). 1820 */ 1821 if (unlikely(ret_code < 0)) 1822 return ret_code; 1823 if (mtd->ecc_strength == 0) 1824 return 0; /* device lacks ecc */ 1825 if (ops->stats) 1826 ops->stats->max_bitflips = ret_code; 1827 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 1828 } 1829 EXPORT_SYMBOL_GPL(mtd_read_oob); 1830 1831 int mtd_write_oob(struct mtd_info *mtd, loff_t to, 1832 struct mtd_oob_ops *ops) 1833 { 1834 struct mtd_info *master = mtd_get_master(mtd); 1835 int ret; 1836 1837 ops->retlen = ops->oobretlen = 0; 1838 1839 if (!(mtd->flags & MTD_WRITEABLE)) 1840 return -EROFS; 1841 1842 ret = mtd_check_oob_ops(mtd, to, ops); 1843 if (ret) 1844 return ret; 1845 1846 ledtrig_mtd_activity(); 1847 1848 /* Check the validity of a potential fallback on mtd->_write */ 1849 if (!master->_write_oob && (!master->_write || ops->oobbuf)) 1850 return -EOPNOTSUPP; 1851 1852 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 1853 return mtd_io_emulated_slc(mtd, to, false, ops); 1854 1855 return mtd_write_oob_std(mtd, to, ops); 1856 } 1857 EXPORT_SYMBOL_GPL(mtd_write_oob); 1858 1859 /** 1860 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section 1861 * @mtd: MTD device structure 1862 * @section: ECC section. Depending on the layout you may have all the ECC 1863 * bytes stored in a single contiguous section, or one section 1864 * per ECC chunk (and sometime several sections for a single ECC 1865 * ECC chunk) 1866 * @oobecc: OOB region struct filled with the appropriate ECC position 1867 * information 1868 * 1869 * This function returns ECC section information in the OOB area. If you want 1870 * to get all the ECC bytes information, then you should call 1871 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. 1872 * 1873 * Returns zero on success, a negative error code otherwise. 1874 */ 1875 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, 1876 struct mtd_oob_region *oobecc) 1877 { 1878 struct mtd_info *master = mtd_get_master(mtd); 1879 1880 memset(oobecc, 0, sizeof(*oobecc)); 1881 1882 if (!master || section < 0) 1883 return -EINVAL; 1884 1885 if (!master->ooblayout || !master->ooblayout->ecc) 1886 return -ENOTSUPP; 1887 1888 return master->ooblayout->ecc(master, section, oobecc); 1889 } 1890 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); 1891 1892 /** 1893 * mtd_ooblayout_free - Get the OOB region definition of a specific free 1894 * section 1895 * @mtd: MTD device structure 1896 * @section: Free section you are interested in. Depending on the layout 1897 * you may have all the free bytes stored in a single contiguous 1898 * section, or one section per ECC chunk plus an extra section 1899 * for the remaining bytes (or other funky layout). 1900 * @oobfree: OOB region struct filled with the appropriate free position 1901 * information 1902 * 1903 * This function returns free bytes position in the OOB area. If you want 1904 * to get all the free bytes information, then you should call 1905 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. 1906 * 1907 * Returns zero on success, a negative error code otherwise. 1908 */ 1909 int mtd_ooblayout_free(struct mtd_info *mtd, int section, 1910 struct mtd_oob_region *oobfree) 1911 { 1912 struct mtd_info *master = mtd_get_master(mtd); 1913 1914 memset(oobfree, 0, sizeof(*oobfree)); 1915 1916 if (!master || section < 0) 1917 return -EINVAL; 1918 1919 if (!master->ooblayout || !master->ooblayout->free) 1920 return -ENOTSUPP; 1921 1922 return master->ooblayout->free(master, section, oobfree); 1923 } 1924 EXPORT_SYMBOL_GPL(mtd_ooblayout_free); 1925 1926 /** 1927 * mtd_ooblayout_find_region - Find the region attached to a specific byte 1928 * @mtd: mtd info structure 1929 * @byte: the byte we are searching for 1930 * @sectionp: pointer where the section id will be stored 1931 * @oobregion: used to retrieve the ECC position 1932 * @iter: iterator function. Should be either mtd_ooblayout_free or 1933 * mtd_ooblayout_ecc depending on the region type you're searching for 1934 * 1935 * This function returns the section id and oobregion information of a 1936 * specific byte. For example, say you want to know where the 4th ECC byte is 1937 * stored, you'll use: 1938 * 1939 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); 1940 * 1941 * Returns zero on success, a negative error code otherwise. 1942 */ 1943 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, 1944 int *sectionp, struct mtd_oob_region *oobregion, 1945 int (*iter)(struct mtd_info *, 1946 int section, 1947 struct mtd_oob_region *oobregion)) 1948 { 1949 int pos = 0, ret, section = 0; 1950 1951 memset(oobregion, 0, sizeof(*oobregion)); 1952 1953 while (1) { 1954 ret = iter(mtd, section, oobregion); 1955 if (ret) 1956 return ret; 1957 1958 if (pos + oobregion->length > byte) 1959 break; 1960 1961 pos += oobregion->length; 1962 section++; 1963 } 1964 1965 /* 1966 * Adjust region info to make it start at the beginning at the 1967 * 'start' ECC byte. 1968 */ 1969 oobregion->offset += byte - pos; 1970 oobregion->length -= byte - pos; 1971 *sectionp = section; 1972 1973 return 0; 1974 } 1975 1976 /** 1977 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific 1978 * ECC byte 1979 * @mtd: mtd info structure 1980 * @eccbyte: the byte we are searching for 1981 * @section: pointer where the section id will be stored 1982 * @oobregion: OOB region information 1983 * 1984 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC 1985 * byte. 1986 * 1987 * Returns zero on success, a negative error code otherwise. 1988 */ 1989 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, 1990 int *section, 1991 struct mtd_oob_region *oobregion) 1992 { 1993 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, 1994 mtd_ooblayout_ecc); 1995 } 1996 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); 1997 1998 /** 1999 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer 2000 * @mtd: mtd info structure 2001 * @buf: destination buffer to store OOB bytes 2002 * @oobbuf: OOB buffer 2003 * @start: first byte to retrieve 2004 * @nbytes: number of bytes to retrieve 2005 * @iter: section iterator 2006 * 2007 * Extract bytes attached to a specific category (ECC or free) 2008 * from the OOB buffer and copy them into buf. 2009 * 2010 * Returns zero on success, a negative error code otherwise. 2011 */ 2012 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, 2013 const u8 *oobbuf, int start, int nbytes, 2014 int (*iter)(struct mtd_info *, 2015 int section, 2016 struct mtd_oob_region *oobregion)) 2017 { 2018 struct mtd_oob_region oobregion; 2019 int section, ret; 2020 2021 ret = mtd_ooblayout_find_region(mtd, start, §ion, 2022 &oobregion, iter); 2023 2024 while (!ret) { 2025 int cnt; 2026 2027 cnt = min_t(int, nbytes, oobregion.length); 2028 memcpy(buf, oobbuf + oobregion.offset, cnt); 2029 buf += cnt; 2030 nbytes -= cnt; 2031 2032 if (!nbytes) 2033 break; 2034 2035 ret = iter(mtd, ++section, &oobregion); 2036 } 2037 2038 return ret; 2039 } 2040 2041 /** 2042 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer 2043 * @mtd: mtd info structure 2044 * @buf: source buffer to get OOB bytes from 2045 * @oobbuf: OOB buffer 2046 * @start: first OOB byte to set 2047 * @nbytes: number of OOB bytes to set 2048 * @iter: section iterator 2049 * 2050 * Fill the OOB buffer with data provided in buf. The category (ECC or free) 2051 * is selected by passing the appropriate iterator. 2052 * 2053 * Returns zero on success, a negative error code otherwise. 2054 */ 2055 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, 2056 u8 *oobbuf, int start, int nbytes, 2057 int (*iter)(struct mtd_info *, 2058 int section, 2059 struct mtd_oob_region *oobregion)) 2060 { 2061 struct mtd_oob_region oobregion; 2062 int section, ret; 2063 2064 ret = mtd_ooblayout_find_region(mtd, start, §ion, 2065 &oobregion, iter); 2066 2067 while (!ret) { 2068 int cnt; 2069 2070 cnt = min_t(int, nbytes, oobregion.length); 2071 memcpy(oobbuf + oobregion.offset, buf, cnt); 2072 buf += cnt; 2073 nbytes -= cnt; 2074 2075 if (!nbytes) 2076 break; 2077 2078 ret = iter(mtd, ++section, &oobregion); 2079 } 2080 2081 return ret; 2082 } 2083 2084 /** 2085 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category 2086 * @mtd: mtd info structure 2087 * @iter: category iterator 2088 * 2089 * Count the number of bytes in a given category. 2090 * 2091 * Returns a positive value on success, a negative error code otherwise. 2092 */ 2093 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, 2094 int (*iter)(struct mtd_info *, 2095 int section, 2096 struct mtd_oob_region *oobregion)) 2097 { 2098 struct mtd_oob_region oobregion; 2099 int section = 0, ret, nbytes = 0; 2100 2101 while (1) { 2102 ret = iter(mtd, section++, &oobregion); 2103 if (ret) { 2104 if (ret == -ERANGE) 2105 ret = nbytes; 2106 break; 2107 } 2108 2109 nbytes += oobregion.length; 2110 } 2111 2112 return ret; 2113 } 2114 2115 /** 2116 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer 2117 * @mtd: mtd info structure 2118 * @eccbuf: destination buffer to store ECC bytes 2119 * @oobbuf: OOB buffer 2120 * @start: first ECC byte to retrieve 2121 * @nbytes: number of ECC bytes to retrieve 2122 * 2123 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. 2124 * 2125 * Returns zero on success, a negative error code otherwise. 2126 */ 2127 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, 2128 const u8 *oobbuf, int start, int nbytes) 2129 { 2130 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, 2131 mtd_ooblayout_ecc); 2132 } 2133 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); 2134 2135 /** 2136 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer 2137 * @mtd: mtd info structure 2138 * @eccbuf: source buffer to get ECC bytes from 2139 * @oobbuf: OOB buffer 2140 * @start: first ECC byte to set 2141 * @nbytes: number of ECC bytes to set 2142 * 2143 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. 2144 * 2145 * Returns zero on success, a negative error code otherwise. 2146 */ 2147 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, 2148 u8 *oobbuf, int start, int nbytes) 2149 { 2150 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, 2151 mtd_ooblayout_ecc); 2152 } 2153 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); 2154 2155 /** 2156 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer 2157 * @mtd: mtd info structure 2158 * @databuf: destination buffer to store ECC bytes 2159 * @oobbuf: OOB buffer 2160 * @start: first ECC byte to retrieve 2161 * @nbytes: number of ECC bytes to retrieve 2162 * 2163 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. 2164 * 2165 * Returns zero on success, a negative error code otherwise. 2166 */ 2167 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, 2168 const u8 *oobbuf, int start, int nbytes) 2169 { 2170 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, 2171 mtd_ooblayout_free); 2172 } 2173 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); 2174 2175 /** 2176 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer 2177 * @mtd: mtd info structure 2178 * @databuf: source buffer to get data bytes from 2179 * @oobbuf: OOB buffer 2180 * @start: first ECC byte to set 2181 * @nbytes: number of ECC bytes to set 2182 * 2183 * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes. 2184 * 2185 * Returns zero on success, a negative error code otherwise. 2186 */ 2187 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, 2188 u8 *oobbuf, int start, int nbytes) 2189 { 2190 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, 2191 mtd_ooblayout_free); 2192 } 2193 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); 2194 2195 /** 2196 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB 2197 * @mtd: mtd info structure 2198 * 2199 * Works like mtd_ooblayout_count_bytes(), except it count free bytes. 2200 * 2201 * Returns zero on success, a negative error code otherwise. 2202 */ 2203 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) 2204 { 2205 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); 2206 } 2207 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); 2208 2209 /** 2210 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB 2211 * @mtd: mtd info structure 2212 * 2213 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. 2214 * 2215 * Returns zero on success, a negative error code otherwise. 2216 */ 2217 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) 2218 { 2219 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); 2220 } 2221 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); 2222 2223 /* 2224 * Method to access the protection register area, present in some flash 2225 * devices. The user data is one time programmable but the factory data is read 2226 * only. 2227 */ 2228 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 2229 struct otp_info *buf) 2230 { 2231 struct mtd_info *master = mtd_get_master(mtd); 2232 2233 if (!master->_get_fact_prot_info) 2234 return -EOPNOTSUPP; 2235 if (!len) 2236 return 0; 2237 return master->_get_fact_prot_info(master, len, retlen, buf); 2238 } 2239 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); 2240 2241 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 2242 size_t *retlen, u_char *buf) 2243 { 2244 struct mtd_info *master = mtd_get_master(mtd); 2245 2246 *retlen = 0; 2247 if (!master->_read_fact_prot_reg) 2248 return -EOPNOTSUPP; 2249 if (!len) 2250 return 0; 2251 return master->_read_fact_prot_reg(master, from, len, retlen, buf); 2252 } 2253 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); 2254 2255 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 2256 struct otp_info *buf) 2257 { 2258 struct mtd_info *master = mtd_get_master(mtd); 2259 2260 if (!master->_get_user_prot_info) 2261 return -EOPNOTSUPP; 2262 if (!len) 2263 return 0; 2264 return master->_get_user_prot_info(master, len, retlen, buf); 2265 } 2266 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); 2267 2268 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 2269 size_t *retlen, u_char *buf) 2270 { 2271 struct mtd_info *master = mtd_get_master(mtd); 2272 2273 *retlen = 0; 2274 if (!master->_read_user_prot_reg) 2275 return -EOPNOTSUPP; 2276 if (!len) 2277 return 0; 2278 return master->_read_user_prot_reg(master, from, len, retlen, buf); 2279 } 2280 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); 2281 2282 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 2283 size_t *retlen, const u_char *buf) 2284 { 2285 struct mtd_info *master = mtd_get_master(mtd); 2286 int ret; 2287 2288 *retlen = 0; 2289 if (!master->_write_user_prot_reg) 2290 return -EOPNOTSUPP; 2291 if (!len) 2292 return 0; 2293 ret = master->_write_user_prot_reg(master, to, len, retlen, buf); 2294 if (ret) 2295 return ret; 2296 2297 /* 2298 * If no data could be written at all, we are out of memory and 2299 * must return -ENOSPC. 2300 */ 2301 return (*retlen) ? 0 : -ENOSPC; 2302 } 2303 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); 2304 2305 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 2306 { 2307 struct mtd_info *master = mtd_get_master(mtd); 2308 2309 if (!master->_lock_user_prot_reg) 2310 return -EOPNOTSUPP; 2311 if (!len) 2312 return 0; 2313 return master->_lock_user_prot_reg(master, from, len); 2314 } 2315 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); 2316 2317 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 2318 { 2319 struct mtd_info *master = mtd_get_master(mtd); 2320 2321 if (!master->_erase_user_prot_reg) 2322 return -EOPNOTSUPP; 2323 if (!len) 2324 return 0; 2325 return master->_erase_user_prot_reg(master, from, len); 2326 } 2327 EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg); 2328 2329 /* Chip-supported device locking */ 2330 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2331 { 2332 struct mtd_info *master = mtd_get_master(mtd); 2333 2334 if (!master->_lock) 2335 return -EOPNOTSUPP; 2336 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2337 return -EINVAL; 2338 if (!len) 2339 return 0; 2340 2341 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2342 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2343 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2344 } 2345 2346 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); 2347 } 2348 EXPORT_SYMBOL_GPL(mtd_lock); 2349 2350 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2351 { 2352 struct mtd_info *master = mtd_get_master(mtd); 2353 2354 if (!master->_unlock) 2355 return -EOPNOTSUPP; 2356 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2357 return -EINVAL; 2358 if (!len) 2359 return 0; 2360 2361 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2362 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2363 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2364 } 2365 2366 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); 2367 } 2368 EXPORT_SYMBOL_GPL(mtd_unlock); 2369 2370 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2371 { 2372 struct mtd_info *master = mtd_get_master(mtd); 2373 2374 if (!master->_is_locked) 2375 return -EOPNOTSUPP; 2376 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2377 return -EINVAL; 2378 if (!len) 2379 return 0; 2380 2381 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2382 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2383 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2384 } 2385 2386 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); 2387 } 2388 EXPORT_SYMBOL_GPL(mtd_is_locked); 2389 2390 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) 2391 { 2392 struct mtd_info *master = mtd_get_master(mtd); 2393 2394 if (ofs < 0 || ofs >= mtd->size) 2395 return -EINVAL; 2396 if (!master->_block_isreserved) 2397 return 0; 2398 2399 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2400 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2401 2402 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); 2403 } 2404 EXPORT_SYMBOL_GPL(mtd_block_isreserved); 2405 2406 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) 2407 { 2408 struct mtd_info *master = mtd_get_master(mtd); 2409 2410 if (ofs < 0 || ofs >= mtd->size) 2411 return -EINVAL; 2412 if (!master->_block_isbad) 2413 return 0; 2414 2415 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2416 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2417 2418 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); 2419 } 2420 EXPORT_SYMBOL_GPL(mtd_block_isbad); 2421 2422 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) 2423 { 2424 struct mtd_info *master = mtd_get_master(mtd); 2425 loff_t moffs; 2426 int ret; 2427 2428 if (!master->_block_markbad) 2429 return -EOPNOTSUPP; 2430 if (ofs < 0 || ofs >= mtd->size) 2431 return -EINVAL; 2432 if (!(mtd->flags & MTD_WRITEABLE)) 2433 return -EROFS; 2434 2435 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2436 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2437 2438 moffs = mtd_get_master_ofs(mtd, ofs); 2439 2440 if (master->_block_isbad) { 2441 ret = master->_block_isbad(master, moffs); 2442 if (ret > 0) 2443 return 0; 2444 } 2445 2446 ret = master->_block_markbad(master, moffs); 2447 if (ret) 2448 return ret; 2449 2450 while (mtd->parent) { 2451 mtd->ecc_stats.badblocks++; 2452 mtd = mtd->parent; 2453 } 2454 2455 return 0; 2456 } 2457 EXPORT_SYMBOL_GPL(mtd_block_markbad); 2458 ALLOW_ERROR_INJECTION(mtd_block_markbad, ERRNO); 2459 2460 /* 2461 * default_mtd_writev - the default writev method 2462 * @mtd: mtd device description object pointer 2463 * @vecs: the vectors to write 2464 * @count: count of vectors in @vecs 2465 * @to: the MTD device offset to write to 2466 * @retlen: on exit contains the count of bytes written to the MTD device. 2467 * 2468 * This function returns zero in case of success and a negative error code in 2469 * case of failure. 2470 */ 2471 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2472 unsigned long count, loff_t to, size_t *retlen) 2473 { 2474 unsigned long i; 2475 size_t totlen = 0, thislen; 2476 int ret = 0; 2477 2478 for (i = 0; i < count; i++) { 2479 if (!vecs[i].iov_len) 2480 continue; 2481 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, 2482 vecs[i].iov_base); 2483 totlen += thislen; 2484 if (ret || thislen != vecs[i].iov_len) 2485 break; 2486 to += vecs[i].iov_len; 2487 } 2488 *retlen = totlen; 2489 return ret; 2490 } 2491 2492 /* 2493 * mtd_writev - the vector-based MTD write method 2494 * @mtd: mtd device description object pointer 2495 * @vecs: the vectors to write 2496 * @count: count of vectors in @vecs 2497 * @to: the MTD device offset to write to 2498 * @retlen: on exit contains the count of bytes written to the MTD device. 2499 * 2500 * This function returns zero in case of success and a negative error code in 2501 * case of failure. 2502 */ 2503 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2504 unsigned long count, loff_t to, size_t *retlen) 2505 { 2506 struct mtd_info *master = mtd_get_master(mtd); 2507 2508 *retlen = 0; 2509 if (!(mtd->flags & MTD_WRITEABLE)) 2510 return -EROFS; 2511 2512 if (!master->_writev) 2513 return default_mtd_writev(mtd, vecs, count, to, retlen); 2514 2515 return master->_writev(master, vecs, count, 2516 mtd_get_master_ofs(mtd, to), retlen); 2517 } 2518 EXPORT_SYMBOL_GPL(mtd_writev); 2519 2520 /** 2521 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size 2522 * @mtd: mtd device description object pointer 2523 * @size: a pointer to the ideal or maximum size of the allocation, points 2524 * to the actual allocation size on success. 2525 * 2526 * This routine attempts to allocate a contiguous kernel buffer up to 2527 * the specified size, backing off the size of the request exponentially 2528 * until the request succeeds or until the allocation size falls below 2529 * the system page size. This attempts to make sure it does not adversely 2530 * impact system performance, so when allocating more than one page, we 2531 * ask the memory allocator to avoid re-trying, swapping, writing back 2532 * or performing I/O. 2533 * 2534 * Note, this function also makes sure that the allocated buffer is aligned to 2535 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. 2536 * 2537 * This is called, for example by mtd_{read,write} and jffs2_scan_medium, 2538 * to handle smaller (i.e. degraded) buffer allocations under low- or 2539 * fragmented-memory situations where such reduced allocations, from a 2540 * requested ideal, are allowed. 2541 * 2542 * Returns a pointer to the allocated buffer on success; otherwise, NULL. 2543 */ 2544 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) 2545 { 2546 gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY; 2547 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); 2548 void *kbuf; 2549 2550 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); 2551 2552 while (*size > min_alloc) { 2553 kbuf = kmalloc(*size, flags); 2554 if (kbuf) 2555 return kbuf; 2556 2557 *size >>= 1; 2558 *size = ALIGN(*size, mtd->writesize); 2559 } 2560 2561 /* 2562 * For the last resort allocation allow 'kmalloc()' to do all sorts of 2563 * things (write-back, dropping caches, etc) by using GFP_KERNEL. 2564 */ 2565 return kmalloc(*size, GFP_KERNEL); 2566 } 2567 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); 2568 2569 #ifdef CONFIG_PROC_FS 2570 2571 /*====================================================================*/ 2572 /* Support for /proc/mtd */ 2573 2574 static int mtd_proc_show(struct seq_file *m, void *v) 2575 { 2576 struct mtd_info *mtd; 2577 2578 seq_puts(m, "dev: size erasesize name\n"); 2579 mutex_lock(&mtd_table_mutex); 2580 mtd_for_each_device(mtd) { 2581 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", 2582 mtd->index, (unsigned long long)mtd->size, 2583 mtd->erasesize, mtd->name); 2584 } 2585 mutex_unlock(&mtd_table_mutex); 2586 return 0; 2587 } 2588 #endif /* CONFIG_PROC_FS */ 2589 2590 /*====================================================================*/ 2591 /* Init code */ 2592 2593 static struct backing_dev_info * __init mtd_bdi_init(const char *name) 2594 { 2595 struct backing_dev_info *bdi; 2596 int ret; 2597 2598 bdi = bdi_alloc(NUMA_NO_NODE); 2599 if (!bdi) 2600 return ERR_PTR(-ENOMEM); 2601 bdi->ra_pages = 0; 2602 bdi->io_pages = 0; 2603 2604 /* 2605 * We put '-0' suffix to the name to get the same name format as we 2606 * used to get. Since this is called only once, we get a unique name. 2607 */ 2608 ret = bdi_register(bdi, "%.28s-0", name); 2609 if (ret) 2610 bdi_put(bdi); 2611 2612 return ret ? ERR_PTR(ret) : bdi; 2613 } 2614 2615 static struct proc_dir_entry *proc_mtd; 2616 2617 static int __init init_mtd(void) 2618 { 2619 int ret; 2620 2621 ret = class_register(&mtd_class); 2622 if (ret) 2623 goto err_reg; 2624 2625 mtd_bdi = mtd_bdi_init("mtd"); 2626 if (IS_ERR(mtd_bdi)) { 2627 ret = PTR_ERR(mtd_bdi); 2628 goto err_bdi; 2629 } 2630 2631 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); 2632 2633 ret = init_mtdchar(); 2634 if (ret) 2635 goto out_procfs; 2636 2637 dfs_dir_mtd = debugfs_create_dir("mtd", NULL); 2638 debugfs_create_bool("expert_analysis_mode", 0600, dfs_dir_mtd, 2639 &mtd_expert_analysis_mode); 2640 2641 return 0; 2642 2643 out_procfs: 2644 if (proc_mtd) 2645 remove_proc_entry("mtd", NULL); 2646 bdi_unregister(mtd_bdi); 2647 bdi_put(mtd_bdi); 2648 err_bdi: 2649 class_unregister(&mtd_class); 2650 err_reg: 2651 pr_err("Error registering mtd class or bdi: %d\n", ret); 2652 return ret; 2653 } 2654 2655 static void __exit cleanup_mtd(void) 2656 { 2657 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 2658 mtd_virt_concat_destroy_joins(); 2659 mtd_virt_concat_destroy_items(); 2660 } 2661 debugfs_remove_recursive(dfs_dir_mtd); 2662 cleanup_mtdchar(); 2663 if (proc_mtd) 2664 remove_proc_entry("mtd", NULL); 2665 class_unregister(&mtd_class); 2666 bdi_unregister(mtd_bdi); 2667 bdi_put(mtd_bdi); 2668 idr_destroy(&mtd_idr); 2669 } 2670 2671 module_init(init_mtd); 2672 module_exit(cleanup_mtd); 2673 2674 MODULE_LICENSE("GPL"); 2675 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 2676 MODULE_DESCRIPTION("Core MTD registration and access routines"); 2677