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 /* Don't report error if OTP is not supported. */ 1087 if (err == -EOPNOTSUPP) 1088 return 0; 1089 return dev_err_probe(dev, err, "Failed to register OTP NVMEM device\n"); 1090 } 1091 1092 /** 1093 * mtd_device_parse_register - parse partitions and register an MTD device. 1094 * 1095 * @mtd: the MTD device to register 1096 * @types: the list of MTD partition probes to try, see 1097 * 'parse_mtd_partitions()' for more information 1098 * @parser_data: MTD partition parser-specific data 1099 * @parts: fallback partition information to register, if parsing fails; 1100 * only valid if %nr_parts > %0 1101 * @nr_parts: the number of partitions in parts, if zero then the full 1102 * MTD device is registered if no partition info is found 1103 * 1104 * This function aggregates MTD partitions parsing (done by 1105 * 'parse_mtd_partitions()') and MTD device and partitions registering. It 1106 * basically follows the most common pattern found in many MTD drivers: 1107 * 1108 * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is 1109 * registered first. 1110 * * Then It tries to probe partitions on MTD device @mtd using parsers 1111 * specified in @types (if @types is %NULL, then the default list of parsers 1112 * is used, see 'parse_mtd_partitions()' for more information). If none are 1113 * found this functions tries to fallback to information specified in 1114 * @parts/@nr_parts. 1115 * * If no partitions were found this function just registers the MTD device 1116 * @mtd and exits. 1117 * 1118 * Returns zero in case of success and a negative error code in case of failure. 1119 */ 1120 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, 1121 struct mtd_part_parser_data *parser_data, 1122 const struct mtd_partition *parts, 1123 int nr_parts) 1124 { 1125 int ret, err; 1126 1127 mtd_set_dev_defaults(mtd); 1128 1129 ret = mtd_otp_nvmem_add(mtd); 1130 if (ret) 1131 goto out; 1132 1133 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) { 1134 ret = add_mtd_device(mtd); 1135 if (ret) 1136 goto out; 1137 } 1138 1139 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1140 ret = mtd_virt_concat_node_create(); 1141 if (ret < 0) 1142 goto out; 1143 } 1144 1145 /* Prefer parsed partitions over driver-provided fallback */ 1146 ret = parse_mtd_partitions(mtd, types, parser_data); 1147 if (ret == -EPROBE_DEFER) 1148 goto out; 1149 1150 if (ret > 0) 1151 ret = 0; 1152 else if (nr_parts) 1153 ret = add_mtd_partitions(mtd, parts, nr_parts); 1154 else if (!device_is_registered(&mtd->dev)) 1155 ret = add_mtd_device(mtd); 1156 else 1157 ret = 0; 1158 1159 if (ret) 1160 goto out; 1161 1162 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1163 ret = mtd_virt_concat_create_join(); 1164 if (ret < 0) 1165 goto out; 1166 } 1167 /* 1168 * FIXME: some drivers unfortunately call this function more than once. 1169 * So we have to check if we've already assigned the reboot notifier. 1170 * 1171 * Generally, we can make multiple calls work for most cases, but it 1172 * does cause problems with parse_mtd_partitions() above (e.g., 1173 * cmdlineparts will register partitions more than once). 1174 */ 1175 WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call, 1176 "MTD already registered\n"); 1177 if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) { 1178 mtd->reboot_notifier.notifier_call = mtd_reboot_notifier; 1179 register_reboot_notifier(&mtd->reboot_notifier); 1180 } 1181 1182 out: 1183 if (ret) { 1184 nvmem_unregister(mtd->otp_user_nvmem); 1185 nvmem_unregister(mtd->otp_factory_nvmem); 1186 } 1187 1188 if (ret && device_is_registered(&mtd->dev)) { 1189 err = del_mtd_device(mtd); 1190 if (err) 1191 pr_err("Error when deleting MTD device (%d)\n", err); 1192 } 1193 1194 return ret; 1195 } 1196 EXPORT_SYMBOL_GPL(mtd_device_parse_register); 1197 1198 /** 1199 * mtd_device_unregister - unregister an existing MTD device. 1200 * 1201 * @master: the MTD device to unregister. This will unregister both the master 1202 * and any partitions if registered. 1203 */ 1204 int mtd_device_unregister(struct mtd_info *master) 1205 { 1206 int err; 1207 1208 if (master->_reboot) { 1209 unregister_reboot_notifier(&master->reboot_notifier); 1210 memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier)); 1211 } 1212 1213 nvmem_unregister(master->otp_user_nvmem); 1214 nvmem_unregister(master->otp_factory_nvmem); 1215 1216 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 1217 err = mtd_virt_concat_destroy(master); 1218 if (err) 1219 return err; 1220 } 1221 err = del_mtd_partitions(master); 1222 if (err) 1223 return err; 1224 1225 if (!device_is_registered(&master->dev)) 1226 return 0; 1227 1228 return del_mtd_device(master); 1229 } 1230 EXPORT_SYMBOL_GPL(mtd_device_unregister); 1231 1232 /** 1233 * register_mtd_user - register a 'user' of MTD devices. 1234 * @new: pointer to notifier info structure 1235 * 1236 * Registers a pair of callbacks function to be called upon addition 1237 * or removal of MTD devices. Causes the 'add' callback to be immediately 1238 * invoked for each MTD device currently present in the system. 1239 */ 1240 void register_mtd_user (struct mtd_notifier *new) 1241 { 1242 struct mtd_info *mtd; 1243 1244 mutex_lock(&mtd_table_mutex); 1245 1246 list_add(&new->list, &mtd_notifiers); 1247 1248 __module_get(THIS_MODULE); 1249 1250 mtd_for_each_device(mtd) 1251 new->add(mtd); 1252 1253 mutex_unlock(&mtd_table_mutex); 1254 } 1255 EXPORT_SYMBOL_GPL(register_mtd_user); 1256 1257 /** 1258 * unregister_mtd_user - unregister a 'user' of MTD devices. 1259 * @old: pointer to notifier info structure 1260 * 1261 * Removes a callback function pair from the list of 'users' to be 1262 * notified upon addition or removal of MTD devices. Causes the 1263 * 'remove' callback to be immediately invoked for each MTD device 1264 * currently present in the system. 1265 */ 1266 int unregister_mtd_user (struct mtd_notifier *old) 1267 { 1268 struct mtd_info *mtd; 1269 1270 mutex_lock(&mtd_table_mutex); 1271 1272 module_put(THIS_MODULE); 1273 1274 mtd_for_each_device(mtd) 1275 old->remove(mtd); 1276 1277 list_del(&old->list); 1278 mutex_unlock(&mtd_table_mutex); 1279 return 0; 1280 } 1281 EXPORT_SYMBOL_GPL(unregister_mtd_user); 1282 1283 /** 1284 * get_mtd_device - obtain a validated handle for an MTD device 1285 * @mtd: last known address of the required MTD device 1286 * @num: internal device number of the required MTD device 1287 * 1288 * Given a number and NULL address, return the num'th entry in the device 1289 * table, if any. Given an address and num == -1, search the device table 1290 * for a device with that address and return if it's still present. Given 1291 * both, return the num'th driver only if its address matches. Return 1292 * error code if not. 1293 */ 1294 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 1295 { 1296 struct mtd_info *ret = NULL, *other; 1297 int err = -ENODEV; 1298 1299 mutex_lock(&mtd_table_mutex); 1300 1301 if (num == -1) { 1302 mtd_for_each_device(other) { 1303 if (other == mtd) { 1304 ret = mtd; 1305 break; 1306 } 1307 } 1308 } else if (num >= 0) { 1309 ret = idr_find(&mtd_idr, num); 1310 if (mtd && mtd != ret) 1311 ret = NULL; 1312 } 1313 1314 if (!ret) { 1315 ret = ERR_PTR(err); 1316 goto out; 1317 } 1318 1319 err = __get_mtd_device(ret); 1320 if (err) 1321 ret = ERR_PTR(err); 1322 out: 1323 mutex_unlock(&mtd_table_mutex); 1324 return ret; 1325 } 1326 EXPORT_SYMBOL_GPL(get_mtd_device); 1327 1328 1329 int __get_mtd_device(struct mtd_info *mtd) 1330 { 1331 struct mtd_info *master = mtd_get_master(mtd); 1332 int err; 1333 1334 if (master->_get_device) { 1335 err = master->_get_device(mtd); 1336 if (err) 1337 return err; 1338 } 1339 1340 if (!try_module_get(master->owner)) { 1341 if (master->_put_device) 1342 master->_put_device(master); 1343 return -ENODEV; 1344 } 1345 1346 while (mtd) { 1347 if (mtd != master) 1348 kref_get(&mtd->refcnt); 1349 mtd = mtd->parent; 1350 } 1351 1352 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) 1353 kref_get(&master->refcnt); 1354 1355 return 0; 1356 } 1357 EXPORT_SYMBOL_GPL(__get_mtd_device); 1358 1359 /** 1360 * of_get_mtd_device_by_node - obtain an MTD device associated with a given node 1361 * 1362 * @np: device tree node 1363 */ 1364 struct mtd_info *of_get_mtd_device_by_node(struct device_node *np) 1365 { 1366 struct mtd_info *mtd = NULL; 1367 struct mtd_info *tmp; 1368 int err; 1369 1370 mutex_lock(&mtd_table_mutex); 1371 1372 err = -EPROBE_DEFER; 1373 mtd_for_each_device(tmp) { 1374 if (mtd_get_of_node(tmp) == np) { 1375 mtd = tmp; 1376 err = __get_mtd_device(mtd); 1377 break; 1378 } 1379 } 1380 1381 mutex_unlock(&mtd_table_mutex); 1382 1383 return err ? ERR_PTR(err) : mtd; 1384 } 1385 EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node); 1386 1387 /** 1388 * get_mtd_device_nm - obtain a validated handle for an MTD device by 1389 * device name 1390 * @name: MTD device name to open 1391 * 1392 * This function returns MTD device description structure in case of 1393 * success and an error code in case of failure. 1394 */ 1395 struct mtd_info *get_mtd_device_nm(const char *name) 1396 { 1397 int err = -ENODEV; 1398 struct mtd_info *mtd = NULL, *other; 1399 1400 mutex_lock(&mtd_table_mutex); 1401 1402 mtd_for_each_device(other) { 1403 if (!strcmp(name, other->name)) { 1404 mtd = other; 1405 break; 1406 } 1407 } 1408 1409 if (!mtd) 1410 goto out_unlock; 1411 1412 err = __get_mtd_device(mtd); 1413 if (err) 1414 goto out_unlock; 1415 1416 mutex_unlock(&mtd_table_mutex); 1417 return mtd; 1418 1419 out_unlock: 1420 mutex_unlock(&mtd_table_mutex); 1421 return ERR_PTR(err); 1422 } 1423 EXPORT_SYMBOL_GPL(get_mtd_device_nm); 1424 1425 void put_mtd_device(struct mtd_info *mtd) 1426 { 1427 mutex_lock(&mtd_table_mutex); 1428 __put_mtd_device(mtd); 1429 mutex_unlock(&mtd_table_mutex); 1430 1431 } 1432 EXPORT_SYMBOL_GPL(put_mtd_device); 1433 1434 void __put_mtd_device(struct mtd_info *mtd) 1435 { 1436 struct mtd_info *master = mtd_get_master(mtd); 1437 1438 while (mtd) { 1439 /* kref_put() can relese mtd, so keep a reference mtd->parent */ 1440 struct mtd_info *parent = mtd->parent; 1441 1442 if (mtd != master) 1443 kref_put(&mtd->refcnt, mtd_device_release); 1444 mtd = parent; 1445 } 1446 1447 if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) 1448 kref_put(&master->refcnt, mtd_device_release); 1449 1450 module_put(master->owner); 1451 1452 /* must be the last as master can be freed in the _put_device */ 1453 if (master->_put_device) 1454 master->_put_device(master); 1455 } 1456 EXPORT_SYMBOL_GPL(__put_mtd_device); 1457 1458 /* 1459 * Erase is an synchronous operation. Device drivers are epected to return a 1460 * negative error code if the operation failed and update instr->fail_addr 1461 * to point the portion that was not properly erased. 1462 */ 1463 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 1464 { 1465 struct mtd_info *master = mtd_get_master(mtd); 1466 u64 mst_ofs = mtd_get_master_ofs(mtd, 0); 1467 struct erase_info adjinstr; 1468 int ret; 1469 1470 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 1471 adjinstr = *instr; 1472 1473 if (!mtd->erasesize || !master->_erase) 1474 return -ENOTSUPP; 1475 1476 if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr) 1477 return -EINVAL; 1478 if (!(mtd->flags & MTD_WRITEABLE)) 1479 return -EROFS; 1480 1481 if (!instr->len) 1482 return 0; 1483 1484 ledtrig_mtd_activity(); 1485 1486 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 1487 adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) * 1488 master->erasesize; 1489 adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) * 1490 master->erasesize) - 1491 adjinstr.addr; 1492 } 1493 1494 adjinstr.addr += mst_ofs; 1495 1496 ret = master->_erase(master, &adjinstr); 1497 1498 if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) { 1499 instr->fail_addr = adjinstr.fail_addr - mst_ofs; 1500 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 1501 instr->fail_addr = mtd_div_by_eb(instr->fail_addr, 1502 master); 1503 instr->fail_addr *= mtd->erasesize; 1504 } 1505 } 1506 1507 return ret; 1508 } 1509 EXPORT_SYMBOL_GPL(mtd_erase); 1510 ALLOW_ERROR_INJECTION(mtd_erase, ERRNO); 1511 1512 /* 1513 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. 1514 */ 1515 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 1516 void **virt, resource_size_t *phys) 1517 { 1518 struct mtd_info *master = mtd_get_master(mtd); 1519 1520 *retlen = 0; 1521 *virt = NULL; 1522 if (phys) 1523 *phys = 0; 1524 if (!master->_point) 1525 return -EOPNOTSUPP; 1526 if (from < 0 || from >= mtd->size || len > mtd->size - from) 1527 return -EINVAL; 1528 if (!len) 1529 return 0; 1530 1531 from = mtd_get_master_ofs(mtd, from); 1532 return master->_point(master, from, len, retlen, virt, phys); 1533 } 1534 EXPORT_SYMBOL_GPL(mtd_point); 1535 1536 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 1537 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 1538 { 1539 struct mtd_info *master = mtd_get_master(mtd); 1540 1541 if (!master->_unpoint) 1542 return -EOPNOTSUPP; 1543 if (from < 0 || from >= mtd->size || len > mtd->size - from) 1544 return -EINVAL; 1545 if (!len) 1546 return 0; 1547 return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len); 1548 } 1549 EXPORT_SYMBOL_GPL(mtd_unpoint); 1550 1551 /* 1552 * Allow NOMMU mmap() to directly map the device (if not NULL) 1553 * - return the address to which the offset maps 1554 * - return -ENOSYS to indicate refusal to do the mapping 1555 */ 1556 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 1557 unsigned long offset, unsigned long flags) 1558 { 1559 size_t retlen; 1560 void *virt; 1561 int ret; 1562 1563 ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL); 1564 if (ret) 1565 return ret; 1566 if (retlen != len) { 1567 mtd_unpoint(mtd, offset, retlen); 1568 return -ENOSYS; 1569 } 1570 return (unsigned long)virt; 1571 } 1572 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); 1573 1574 static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master, 1575 const struct mtd_ecc_stats *old_stats) 1576 { 1577 struct mtd_ecc_stats diff; 1578 1579 if (master == mtd) 1580 return; 1581 1582 diff = master->ecc_stats; 1583 diff.failed -= old_stats->failed; 1584 diff.corrected -= old_stats->corrected; 1585 1586 while (mtd->parent) { 1587 mtd->ecc_stats.failed += diff.failed; 1588 mtd->ecc_stats.corrected += diff.corrected; 1589 mtd = mtd->parent; 1590 } 1591 } 1592 1593 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 1594 u_char *buf) 1595 { 1596 struct mtd_oob_ops ops = { 1597 .len = len, 1598 .datbuf = buf, 1599 }; 1600 int ret; 1601 1602 ret = mtd_read_oob(mtd, from, &ops); 1603 *retlen = ops.retlen; 1604 1605 WARN_ON_ONCE(*retlen != len && mtd_is_bitflip_or_eccerr(ret)); 1606 1607 return ret; 1608 } 1609 EXPORT_SYMBOL_GPL(mtd_read); 1610 ALLOW_ERROR_INJECTION(mtd_read, ERRNO); 1611 1612 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 1613 const u_char *buf) 1614 { 1615 struct mtd_oob_ops ops = { 1616 .len = len, 1617 .datbuf = (u8 *)buf, 1618 }; 1619 int ret; 1620 1621 ret = mtd_write_oob(mtd, to, &ops); 1622 *retlen = ops.retlen; 1623 1624 return ret; 1625 } 1626 EXPORT_SYMBOL_GPL(mtd_write); 1627 ALLOW_ERROR_INJECTION(mtd_write, ERRNO); 1628 1629 /* 1630 * In blackbox flight recorder like scenarios we want to make successful writes 1631 * in interrupt context. panic_write() is only intended to be called when its 1632 * known the kernel is about to panic and we need the write to succeed. Since 1633 * the kernel is not going to be running for much longer, this function can 1634 * break locks and delay to ensure the write succeeds (but not sleep). 1635 */ 1636 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 1637 const u_char *buf) 1638 { 1639 struct mtd_info *master = mtd_get_master(mtd); 1640 1641 *retlen = 0; 1642 if (!master->_panic_write) 1643 return -EOPNOTSUPP; 1644 if (to < 0 || to >= mtd->size || len > mtd->size - to) 1645 return -EINVAL; 1646 if (!(mtd->flags & MTD_WRITEABLE)) 1647 return -EROFS; 1648 if (!len) 1649 return 0; 1650 if (!master->oops_panic_write) 1651 master->oops_panic_write = true; 1652 1653 return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len, 1654 retlen, buf); 1655 } 1656 EXPORT_SYMBOL_GPL(mtd_panic_write); 1657 1658 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, 1659 struct mtd_oob_ops *ops) 1660 { 1661 /* 1662 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving 1663 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in 1664 * this case. 1665 */ 1666 if (!ops->datbuf) 1667 ops->len = 0; 1668 1669 if (!ops->oobbuf) 1670 ops->ooblen = 0; 1671 1672 if (offs < 0 || offs + ops->len > mtd->size) 1673 return -EINVAL; 1674 1675 if (ops->ooblen) { 1676 size_t maxooblen; 1677 1678 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) 1679 return -EINVAL; 1680 1681 maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) - 1682 mtd_div_by_ws(offs, mtd)) * 1683 mtd_oobavail(mtd, ops)) - ops->ooboffs; 1684 if (ops->ooblen > maxooblen) 1685 return -EINVAL; 1686 } 1687 1688 return 0; 1689 } 1690 1691 static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from, 1692 struct mtd_oob_ops *ops) 1693 { 1694 struct mtd_info *master = mtd_get_master(mtd); 1695 int ret; 1696 1697 from = mtd_get_master_ofs(mtd, from); 1698 if (master->_read_oob) 1699 ret = master->_read_oob(master, from, ops); 1700 else 1701 ret = master->_read(master, from, ops->len, &ops->retlen, 1702 ops->datbuf); 1703 1704 return ret; 1705 } 1706 1707 static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to, 1708 struct mtd_oob_ops *ops) 1709 { 1710 struct mtd_info *master = mtd_get_master(mtd); 1711 int ret; 1712 1713 to = mtd_get_master_ofs(mtd, to); 1714 if (master->_write_oob) 1715 ret = master->_write_oob(master, to, ops); 1716 else 1717 ret = master->_write(master, to, ops->len, &ops->retlen, 1718 ops->datbuf); 1719 1720 return ret; 1721 } 1722 1723 static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read, 1724 struct mtd_oob_ops *ops) 1725 { 1726 struct mtd_info *master = mtd_get_master(mtd); 1727 int ngroups = mtd_pairing_groups(master); 1728 int npairs = mtd_wunit_per_eb(master) / ngroups; 1729 struct mtd_oob_ops adjops = *ops; 1730 unsigned int wunit, oobavail; 1731 struct mtd_pairing_info info; 1732 int max_bitflips = 0; 1733 u32 ebofs, pageofs; 1734 loff_t base, pos; 1735 1736 ebofs = mtd_mod_by_eb(start, mtd); 1737 base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize; 1738 info.group = 0; 1739 info.pair = mtd_div_by_ws(ebofs, mtd); 1740 pageofs = mtd_mod_by_ws(ebofs, mtd); 1741 oobavail = mtd_oobavail(mtd, ops); 1742 1743 while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) { 1744 int ret; 1745 1746 if (info.pair >= npairs) { 1747 info.pair = 0; 1748 base += master->erasesize; 1749 } 1750 1751 wunit = mtd_pairing_info_to_wunit(master, &info); 1752 pos = mtd_wunit_to_offset(mtd, base, wunit); 1753 1754 adjops.len = ops->len - ops->retlen; 1755 if (adjops.len > mtd->writesize - pageofs) 1756 adjops.len = mtd->writesize - pageofs; 1757 1758 adjops.ooblen = ops->ooblen - ops->oobretlen; 1759 if (adjops.ooblen > oobavail - adjops.ooboffs) 1760 adjops.ooblen = oobavail - adjops.ooboffs; 1761 1762 if (read) { 1763 ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops); 1764 if (ret > 0) 1765 max_bitflips = max(max_bitflips, ret); 1766 } else { 1767 ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops); 1768 } 1769 1770 if (ret < 0) 1771 return ret; 1772 1773 max_bitflips = max(max_bitflips, ret); 1774 ops->retlen += adjops.retlen; 1775 ops->oobretlen += adjops.oobretlen; 1776 adjops.datbuf += adjops.retlen; 1777 adjops.oobbuf += adjops.oobretlen; 1778 adjops.ooboffs = 0; 1779 pageofs = 0; 1780 info.pair++; 1781 } 1782 1783 return max_bitflips; 1784 } 1785 1786 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) 1787 { 1788 struct mtd_info *master = mtd_get_master(mtd); 1789 struct mtd_ecc_stats old_stats = master->ecc_stats; 1790 int ret_code; 1791 1792 ops->retlen = ops->oobretlen = 0; 1793 1794 ret_code = mtd_check_oob_ops(mtd, from, ops); 1795 if (ret_code) 1796 return ret_code; 1797 1798 ledtrig_mtd_activity(); 1799 1800 /* Check the validity of a potential fallback on mtd->_read */ 1801 if (!master->_read_oob && (!master->_read || ops->oobbuf)) 1802 return -EOPNOTSUPP; 1803 1804 if (ops->stats) 1805 memset(ops->stats, 0, sizeof(*ops->stats)); 1806 1807 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 1808 ret_code = mtd_io_emulated_slc(mtd, from, true, ops); 1809 else 1810 ret_code = mtd_read_oob_std(mtd, from, ops); 1811 1812 mtd_update_ecc_stats(mtd, master, &old_stats); 1813 1814 /* 1815 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics 1816 * similar to mtd->_read(), returning a non-negative integer 1817 * representing max bitflips. In other cases, mtd->_read_oob() may 1818 * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). 1819 */ 1820 if (unlikely(ret_code < 0)) 1821 return ret_code; 1822 if (mtd->ecc_strength == 0) 1823 return 0; /* device lacks ecc */ 1824 if (ops->stats) 1825 ops->stats->max_bitflips = ret_code; 1826 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 1827 } 1828 EXPORT_SYMBOL_GPL(mtd_read_oob); 1829 1830 int mtd_write_oob(struct mtd_info *mtd, loff_t to, 1831 struct mtd_oob_ops *ops) 1832 { 1833 struct mtd_info *master = mtd_get_master(mtd); 1834 int ret; 1835 1836 ops->retlen = ops->oobretlen = 0; 1837 1838 if (!(mtd->flags & MTD_WRITEABLE)) 1839 return -EROFS; 1840 1841 ret = mtd_check_oob_ops(mtd, to, ops); 1842 if (ret) 1843 return ret; 1844 1845 ledtrig_mtd_activity(); 1846 1847 /* Check the validity of a potential fallback on mtd->_write */ 1848 if (!master->_write_oob && (!master->_write || ops->oobbuf)) 1849 return -EOPNOTSUPP; 1850 1851 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 1852 return mtd_io_emulated_slc(mtd, to, false, ops); 1853 1854 return mtd_write_oob_std(mtd, to, ops); 1855 } 1856 EXPORT_SYMBOL_GPL(mtd_write_oob); 1857 1858 /** 1859 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section 1860 * @mtd: MTD device structure 1861 * @section: ECC section. Depending on the layout you may have all the ECC 1862 * bytes stored in a single contiguous section, or one section 1863 * per ECC chunk (and sometime several sections for a single ECC 1864 * ECC chunk) 1865 * @oobecc: OOB region struct filled with the appropriate ECC position 1866 * information 1867 * 1868 * This function returns ECC section information in the OOB area. If you want 1869 * to get all the ECC bytes information, then you should call 1870 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. 1871 * 1872 * Returns zero on success, a negative error code otherwise. 1873 */ 1874 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, 1875 struct mtd_oob_region *oobecc) 1876 { 1877 struct mtd_info *master = mtd_get_master(mtd); 1878 1879 memset(oobecc, 0, sizeof(*oobecc)); 1880 1881 if (!master || section < 0) 1882 return -EINVAL; 1883 1884 if (!master->ooblayout || !master->ooblayout->ecc) 1885 return -ENOTSUPP; 1886 1887 return master->ooblayout->ecc(master, section, oobecc); 1888 } 1889 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); 1890 1891 /** 1892 * mtd_ooblayout_free - Get the OOB region definition of a specific free 1893 * section 1894 * @mtd: MTD device structure 1895 * @section: Free section you are interested in. Depending on the layout 1896 * you may have all the free bytes stored in a single contiguous 1897 * section, or one section per ECC chunk plus an extra section 1898 * for the remaining bytes (or other funky layout). 1899 * @oobfree: OOB region struct filled with the appropriate free position 1900 * information 1901 * 1902 * This function returns free bytes position in the OOB area. If you want 1903 * to get all the free bytes information, then you should call 1904 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. 1905 * 1906 * Returns zero on success, a negative error code otherwise. 1907 */ 1908 int mtd_ooblayout_free(struct mtd_info *mtd, int section, 1909 struct mtd_oob_region *oobfree) 1910 { 1911 struct mtd_info *master = mtd_get_master(mtd); 1912 1913 memset(oobfree, 0, sizeof(*oobfree)); 1914 1915 if (!master || section < 0) 1916 return -EINVAL; 1917 1918 if (!master->ooblayout || !master->ooblayout->free) 1919 return -ENOTSUPP; 1920 1921 return master->ooblayout->free(master, section, oobfree); 1922 } 1923 EXPORT_SYMBOL_GPL(mtd_ooblayout_free); 1924 1925 /** 1926 * mtd_ooblayout_find_region - Find the region attached to a specific byte 1927 * @mtd: mtd info structure 1928 * @byte: the byte we are searching for 1929 * @sectionp: pointer where the section id will be stored 1930 * @oobregion: used to retrieve the ECC position 1931 * @iter: iterator function. Should be either mtd_ooblayout_free or 1932 * mtd_ooblayout_ecc depending on the region type you're searching for 1933 * 1934 * This function returns the section id and oobregion information of a 1935 * specific byte. For example, say you want to know where the 4th ECC byte is 1936 * stored, you'll use: 1937 * 1938 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); 1939 * 1940 * Returns zero on success, a negative error code otherwise. 1941 */ 1942 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, 1943 int *sectionp, struct mtd_oob_region *oobregion, 1944 int (*iter)(struct mtd_info *, 1945 int section, 1946 struct mtd_oob_region *oobregion)) 1947 { 1948 int pos = 0, ret, section = 0; 1949 1950 memset(oobregion, 0, sizeof(*oobregion)); 1951 1952 while (1) { 1953 ret = iter(mtd, section, oobregion); 1954 if (ret) 1955 return ret; 1956 1957 if (pos + oobregion->length > byte) 1958 break; 1959 1960 pos += oobregion->length; 1961 section++; 1962 } 1963 1964 /* 1965 * Adjust region info to make it start at the beginning at the 1966 * 'start' ECC byte. 1967 */ 1968 oobregion->offset += byte - pos; 1969 oobregion->length -= byte - pos; 1970 *sectionp = section; 1971 1972 return 0; 1973 } 1974 1975 /** 1976 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific 1977 * ECC byte 1978 * @mtd: mtd info structure 1979 * @eccbyte: the byte we are searching for 1980 * @section: pointer where the section id will be stored 1981 * @oobregion: OOB region information 1982 * 1983 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC 1984 * byte. 1985 * 1986 * Returns zero on success, a negative error code otherwise. 1987 */ 1988 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, 1989 int *section, 1990 struct mtd_oob_region *oobregion) 1991 { 1992 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, 1993 mtd_ooblayout_ecc); 1994 } 1995 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); 1996 1997 /** 1998 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer 1999 * @mtd: mtd info structure 2000 * @buf: destination buffer to store OOB bytes 2001 * @oobbuf: OOB buffer 2002 * @start: first byte to retrieve 2003 * @nbytes: number of bytes to retrieve 2004 * @iter: section iterator 2005 * 2006 * Extract bytes attached to a specific category (ECC or free) 2007 * from the OOB buffer and copy them into buf. 2008 * 2009 * Returns zero on success, a negative error code otherwise. 2010 */ 2011 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, 2012 const u8 *oobbuf, int start, int nbytes, 2013 int (*iter)(struct mtd_info *, 2014 int section, 2015 struct mtd_oob_region *oobregion)) 2016 { 2017 struct mtd_oob_region oobregion; 2018 int section, ret; 2019 2020 ret = mtd_ooblayout_find_region(mtd, start, §ion, 2021 &oobregion, iter); 2022 2023 while (!ret) { 2024 int cnt; 2025 2026 cnt = min_t(int, nbytes, oobregion.length); 2027 memcpy(buf, oobbuf + oobregion.offset, cnt); 2028 buf += cnt; 2029 nbytes -= cnt; 2030 2031 if (!nbytes) 2032 break; 2033 2034 ret = iter(mtd, ++section, &oobregion); 2035 } 2036 2037 return ret; 2038 } 2039 2040 /** 2041 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer 2042 * @mtd: mtd info structure 2043 * @buf: source buffer to get OOB bytes from 2044 * @oobbuf: OOB buffer 2045 * @start: first OOB byte to set 2046 * @nbytes: number of OOB bytes to set 2047 * @iter: section iterator 2048 * 2049 * Fill the OOB buffer with data provided in buf. The category (ECC or free) 2050 * is selected by passing the appropriate iterator. 2051 * 2052 * Returns zero on success, a negative error code otherwise. 2053 */ 2054 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, 2055 u8 *oobbuf, int start, int nbytes, 2056 int (*iter)(struct mtd_info *, 2057 int section, 2058 struct mtd_oob_region *oobregion)) 2059 { 2060 struct mtd_oob_region oobregion; 2061 int section, ret; 2062 2063 ret = mtd_ooblayout_find_region(mtd, start, §ion, 2064 &oobregion, iter); 2065 2066 while (!ret) { 2067 int cnt; 2068 2069 cnt = min_t(int, nbytes, oobregion.length); 2070 memcpy(oobbuf + oobregion.offset, buf, cnt); 2071 buf += cnt; 2072 nbytes -= cnt; 2073 2074 if (!nbytes) 2075 break; 2076 2077 ret = iter(mtd, ++section, &oobregion); 2078 } 2079 2080 return ret; 2081 } 2082 2083 /** 2084 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category 2085 * @mtd: mtd info structure 2086 * @iter: category iterator 2087 * 2088 * Count the number of bytes in a given category. 2089 * 2090 * Returns a positive value on success, a negative error code otherwise. 2091 */ 2092 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, 2093 int (*iter)(struct mtd_info *, 2094 int section, 2095 struct mtd_oob_region *oobregion)) 2096 { 2097 struct mtd_oob_region oobregion; 2098 int section = 0, ret, nbytes = 0; 2099 2100 while (1) { 2101 ret = iter(mtd, section++, &oobregion); 2102 if (ret) { 2103 if (ret == -ERANGE) 2104 ret = nbytes; 2105 break; 2106 } 2107 2108 nbytes += oobregion.length; 2109 } 2110 2111 return ret; 2112 } 2113 2114 /** 2115 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer 2116 * @mtd: mtd info structure 2117 * @eccbuf: destination buffer to store ECC bytes 2118 * @oobbuf: OOB buffer 2119 * @start: first ECC byte to retrieve 2120 * @nbytes: number of ECC bytes to retrieve 2121 * 2122 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. 2123 * 2124 * Returns zero on success, a negative error code otherwise. 2125 */ 2126 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, 2127 const u8 *oobbuf, int start, int nbytes) 2128 { 2129 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, 2130 mtd_ooblayout_ecc); 2131 } 2132 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); 2133 2134 /** 2135 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer 2136 * @mtd: mtd info structure 2137 * @eccbuf: source buffer to get ECC bytes from 2138 * @oobbuf: OOB buffer 2139 * @start: first ECC byte to set 2140 * @nbytes: number of ECC bytes to set 2141 * 2142 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. 2143 * 2144 * Returns zero on success, a negative error code otherwise. 2145 */ 2146 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, 2147 u8 *oobbuf, int start, int nbytes) 2148 { 2149 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, 2150 mtd_ooblayout_ecc); 2151 } 2152 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); 2153 2154 /** 2155 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer 2156 * @mtd: mtd info structure 2157 * @databuf: destination buffer to store ECC bytes 2158 * @oobbuf: OOB buffer 2159 * @start: first ECC byte to retrieve 2160 * @nbytes: number of ECC bytes to retrieve 2161 * 2162 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. 2163 * 2164 * Returns zero on success, a negative error code otherwise. 2165 */ 2166 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, 2167 const u8 *oobbuf, int start, int nbytes) 2168 { 2169 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, 2170 mtd_ooblayout_free); 2171 } 2172 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); 2173 2174 /** 2175 * mtd_ooblayout_set_databytes - set data bytes into the oob buffer 2176 * @mtd: mtd info structure 2177 * @databuf: source buffer to get data bytes from 2178 * @oobbuf: OOB buffer 2179 * @start: first ECC byte to set 2180 * @nbytes: number of ECC bytes to set 2181 * 2182 * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes. 2183 * 2184 * Returns zero on success, a negative error code otherwise. 2185 */ 2186 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, 2187 u8 *oobbuf, int start, int nbytes) 2188 { 2189 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, 2190 mtd_ooblayout_free); 2191 } 2192 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); 2193 2194 /** 2195 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB 2196 * @mtd: mtd info structure 2197 * 2198 * Works like mtd_ooblayout_count_bytes(), except it count free bytes. 2199 * 2200 * Returns zero on success, a negative error code otherwise. 2201 */ 2202 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) 2203 { 2204 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); 2205 } 2206 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); 2207 2208 /** 2209 * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB 2210 * @mtd: mtd info structure 2211 * 2212 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. 2213 * 2214 * Returns zero on success, a negative error code otherwise. 2215 */ 2216 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) 2217 { 2218 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); 2219 } 2220 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); 2221 2222 /* 2223 * Method to access the protection register area, present in some flash 2224 * devices. The user data is one time programmable but the factory data is read 2225 * only. 2226 */ 2227 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 2228 struct otp_info *buf) 2229 { 2230 struct mtd_info *master = mtd_get_master(mtd); 2231 2232 if (!master->_get_fact_prot_info) 2233 return -EOPNOTSUPP; 2234 if (!len) 2235 return 0; 2236 return master->_get_fact_prot_info(master, len, retlen, buf); 2237 } 2238 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); 2239 2240 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 2241 size_t *retlen, u_char *buf) 2242 { 2243 struct mtd_info *master = mtd_get_master(mtd); 2244 2245 *retlen = 0; 2246 if (!master->_read_fact_prot_reg) 2247 return -EOPNOTSUPP; 2248 if (!len) 2249 return 0; 2250 return master->_read_fact_prot_reg(master, from, len, retlen, buf); 2251 } 2252 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); 2253 2254 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 2255 struct otp_info *buf) 2256 { 2257 struct mtd_info *master = mtd_get_master(mtd); 2258 2259 if (!master->_get_user_prot_info) 2260 return -EOPNOTSUPP; 2261 if (!len) 2262 return 0; 2263 return master->_get_user_prot_info(master, len, retlen, buf); 2264 } 2265 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); 2266 2267 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 2268 size_t *retlen, u_char *buf) 2269 { 2270 struct mtd_info *master = mtd_get_master(mtd); 2271 2272 *retlen = 0; 2273 if (!master->_read_user_prot_reg) 2274 return -EOPNOTSUPP; 2275 if (!len) 2276 return 0; 2277 return master->_read_user_prot_reg(master, from, len, retlen, buf); 2278 } 2279 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); 2280 2281 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 2282 size_t *retlen, const u_char *buf) 2283 { 2284 struct mtd_info *master = mtd_get_master(mtd); 2285 int ret; 2286 2287 *retlen = 0; 2288 if (!master->_write_user_prot_reg) 2289 return -EOPNOTSUPP; 2290 if (!len) 2291 return 0; 2292 ret = master->_write_user_prot_reg(master, to, len, retlen, buf); 2293 if (ret) 2294 return ret; 2295 2296 /* 2297 * If no data could be written at all, we are out of memory and 2298 * must return -ENOSPC. 2299 */ 2300 return (*retlen) ? 0 : -ENOSPC; 2301 } 2302 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); 2303 2304 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 2305 { 2306 struct mtd_info *master = mtd_get_master(mtd); 2307 2308 if (!master->_lock_user_prot_reg) 2309 return -EOPNOTSUPP; 2310 if (!len) 2311 return 0; 2312 return master->_lock_user_prot_reg(master, from, len); 2313 } 2314 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); 2315 2316 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 2317 { 2318 struct mtd_info *master = mtd_get_master(mtd); 2319 2320 if (!master->_erase_user_prot_reg) 2321 return -EOPNOTSUPP; 2322 if (!len) 2323 return 0; 2324 return master->_erase_user_prot_reg(master, from, len); 2325 } 2326 EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg); 2327 2328 /* Chip-supported device locking */ 2329 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2330 { 2331 struct mtd_info *master = mtd_get_master(mtd); 2332 2333 if (!master->_lock) 2334 return -EOPNOTSUPP; 2335 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2336 return -EINVAL; 2337 if (!len) 2338 return 0; 2339 2340 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2341 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2342 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2343 } 2344 2345 return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len); 2346 } 2347 EXPORT_SYMBOL_GPL(mtd_lock); 2348 2349 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2350 { 2351 struct mtd_info *master = mtd_get_master(mtd); 2352 2353 if (!master->_unlock) 2354 return -EOPNOTSUPP; 2355 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2356 return -EINVAL; 2357 if (!len) 2358 return 0; 2359 2360 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2361 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2362 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2363 } 2364 2365 return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len); 2366 } 2367 EXPORT_SYMBOL_GPL(mtd_unlock); 2368 2369 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 2370 { 2371 struct mtd_info *master = mtd_get_master(mtd); 2372 2373 if (!master->_is_locked) 2374 return -EOPNOTSUPP; 2375 if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs) 2376 return -EINVAL; 2377 if (!len) 2378 return 0; 2379 2380 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) { 2381 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2382 len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize; 2383 } 2384 2385 return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len); 2386 } 2387 EXPORT_SYMBOL_GPL(mtd_is_locked); 2388 2389 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) 2390 { 2391 struct mtd_info *master = mtd_get_master(mtd); 2392 2393 if (ofs < 0 || ofs >= mtd->size) 2394 return -EINVAL; 2395 if (!master->_block_isreserved) 2396 return 0; 2397 2398 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2399 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2400 2401 return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs)); 2402 } 2403 EXPORT_SYMBOL_GPL(mtd_block_isreserved); 2404 2405 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) 2406 { 2407 struct mtd_info *master = mtd_get_master(mtd); 2408 2409 if (ofs < 0 || ofs >= mtd->size) 2410 return -EINVAL; 2411 if (!master->_block_isbad) 2412 return 0; 2413 2414 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2415 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2416 2417 return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs)); 2418 } 2419 EXPORT_SYMBOL_GPL(mtd_block_isbad); 2420 2421 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) 2422 { 2423 struct mtd_info *master = mtd_get_master(mtd); 2424 loff_t moffs; 2425 int ret; 2426 2427 if (!master->_block_markbad) 2428 return -EOPNOTSUPP; 2429 if (ofs < 0 || ofs >= mtd->size) 2430 return -EINVAL; 2431 if (!(mtd->flags & MTD_WRITEABLE)) 2432 return -EROFS; 2433 2434 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) 2435 ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize; 2436 2437 moffs = mtd_get_master_ofs(mtd, ofs); 2438 2439 if (master->_block_isbad) { 2440 ret = master->_block_isbad(master, moffs); 2441 if (ret > 0) 2442 return 0; 2443 } 2444 2445 ret = master->_block_markbad(master, moffs); 2446 if (ret) 2447 return ret; 2448 2449 while (mtd->parent) { 2450 mtd->ecc_stats.badblocks++; 2451 mtd = mtd->parent; 2452 } 2453 2454 return 0; 2455 } 2456 EXPORT_SYMBOL_GPL(mtd_block_markbad); 2457 ALLOW_ERROR_INJECTION(mtd_block_markbad, ERRNO); 2458 2459 /* 2460 * default_mtd_writev - the default writev method 2461 * @mtd: mtd device description object pointer 2462 * @vecs: the vectors to write 2463 * @count: count of vectors in @vecs 2464 * @to: the MTD device offset to write to 2465 * @retlen: on exit contains the count of bytes written to the MTD device. 2466 * 2467 * This function returns zero in case of success and a negative error code in 2468 * case of failure. 2469 */ 2470 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2471 unsigned long count, loff_t to, size_t *retlen) 2472 { 2473 unsigned long i; 2474 size_t totlen = 0, thislen; 2475 int ret = 0; 2476 2477 for (i = 0; i < count; i++) { 2478 if (!vecs[i].iov_len) 2479 continue; 2480 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, 2481 vecs[i].iov_base); 2482 totlen += thislen; 2483 if (ret || thislen != vecs[i].iov_len) 2484 break; 2485 to += vecs[i].iov_len; 2486 } 2487 *retlen = totlen; 2488 return ret; 2489 } 2490 2491 /* 2492 * mtd_writev - the vector-based MTD write method 2493 * @mtd: mtd device description object pointer 2494 * @vecs: the vectors to write 2495 * @count: count of vectors in @vecs 2496 * @to: the MTD device offset to write to 2497 * @retlen: on exit contains the count of bytes written to the MTD device. 2498 * 2499 * This function returns zero in case of success and a negative error code in 2500 * case of failure. 2501 */ 2502 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 2503 unsigned long count, loff_t to, size_t *retlen) 2504 { 2505 struct mtd_info *master = mtd_get_master(mtd); 2506 2507 *retlen = 0; 2508 if (!(mtd->flags & MTD_WRITEABLE)) 2509 return -EROFS; 2510 2511 if (!master->_writev) 2512 return default_mtd_writev(mtd, vecs, count, to, retlen); 2513 2514 return master->_writev(master, vecs, count, 2515 mtd_get_master_ofs(mtd, to), retlen); 2516 } 2517 EXPORT_SYMBOL_GPL(mtd_writev); 2518 2519 /** 2520 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size 2521 * @mtd: mtd device description object pointer 2522 * @size: a pointer to the ideal or maximum size of the allocation, points 2523 * to the actual allocation size on success. 2524 * 2525 * This routine attempts to allocate a contiguous kernel buffer up to 2526 * the specified size, backing off the size of the request exponentially 2527 * until the request succeeds or until the allocation size falls below 2528 * the system page size. This attempts to make sure it does not adversely 2529 * impact system performance, so when allocating more than one page, we 2530 * ask the memory allocator to avoid re-trying, swapping, writing back 2531 * or performing I/O. 2532 * 2533 * Note, this function also makes sure that the allocated buffer is aligned to 2534 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. 2535 * 2536 * This is called, for example by mtd_{read,write} and jffs2_scan_medium, 2537 * to handle smaller (i.e. degraded) buffer allocations under low- or 2538 * fragmented-memory situations where such reduced allocations, from a 2539 * requested ideal, are allowed. 2540 * 2541 * Returns a pointer to the allocated buffer on success; otherwise, NULL. 2542 */ 2543 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) 2544 { 2545 gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY; 2546 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); 2547 void *kbuf; 2548 2549 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); 2550 2551 while (*size > min_alloc) { 2552 kbuf = kmalloc(*size, flags); 2553 if (kbuf) 2554 return kbuf; 2555 2556 *size >>= 1; 2557 *size = ALIGN(*size, mtd->writesize); 2558 } 2559 2560 /* 2561 * For the last resort allocation allow 'kmalloc()' to do all sorts of 2562 * things (write-back, dropping caches, etc) by using GFP_KERNEL. 2563 */ 2564 return kmalloc(*size, GFP_KERNEL); 2565 } 2566 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); 2567 2568 #ifdef CONFIG_PROC_FS 2569 2570 /*====================================================================*/ 2571 /* Support for /proc/mtd */ 2572 2573 static int mtd_proc_show(struct seq_file *m, void *v) 2574 { 2575 struct mtd_info *mtd; 2576 2577 seq_puts(m, "dev: size erasesize name\n"); 2578 mutex_lock(&mtd_table_mutex); 2579 mtd_for_each_device(mtd) { 2580 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", 2581 mtd->index, (unsigned long long)mtd->size, 2582 mtd->erasesize, mtd->name); 2583 } 2584 mutex_unlock(&mtd_table_mutex); 2585 return 0; 2586 } 2587 #endif /* CONFIG_PROC_FS */ 2588 2589 /*====================================================================*/ 2590 /* Init code */ 2591 2592 static struct backing_dev_info * __init mtd_bdi_init(const char *name) 2593 { 2594 struct backing_dev_info *bdi; 2595 int ret; 2596 2597 bdi = bdi_alloc(NUMA_NO_NODE); 2598 if (!bdi) 2599 return ERR_PTR(-ENOMEM); 2600 bdi->ra_pages = 0; 2601 bdi->io_pages = 0; 2602 2603 /* 2604 * We put '-0' suffix to the name to get the same name format as we 2605 * used to get. Since this is called only once, we get a unique name. 2606 */ 2607 ret = bdi_register(bdi, "%.28s-0", name); 2608 if (ret) 2609 bdi_put(bdi); 2610 2611 return ret ? ERR_PTR(ret) : bdi; 2612 } 2613 2614 static struct proc_dir_entry *proc_mtd; 2615 2616 static int __init init_mtd(void) 2617 { 2618 int ret; 2619 2620 ret = class_register(&mtd_class); 2621 if (ret) 2622 goto err_reg; 2623 2624 mtd_bdi = mtd_bdi_init("mtd"); 2625 if (IS_ERR(mtd_bdi)) { 2626 ret = PTR_ERR(mtd_bdi); 2627 goto err_bdi; 2628 } 2629 2630 proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show); 2631 2632 ret = init_mtdchar(); 2633 if (ret) 2634 goto out_procfs; 2635 2636 dfs_dir_mtd = debugfs_create_dir("mtd", NULL); 2637 debugfs_create_bool("expert_analysis_mode", 0600, dfs_dir_mtd, 2638 &mtd_expert_analysis_mode); 2639 2640 return 0; 2641 2642 out_procfs: 2643 if (proc_mtd) 2644 remove_proc_entry("mtd", NULL); 2645 bdi_unregister(mtd_bdi); 2646 bdi_put(mtd_bdi); 2647 err_bdi: 2648 class_unregister(&mtd_class); 2649 err_reg: 2650 pr_err("Error registering mtd class or bdi: %d\n", ret); 2651 return ret; 2652 } 2653 2654 static void __exit cleanup_mtd(void) 2655 { 2656 if (IS_REACHABLE(CONFIG_MTD_VIRT_CONCAT)) { 2657 mtd_virt_concat_destroy_joins(); 2658 mtd_virt_concat_destroy_items(); 2659 } 2660 debugfs_remove_recursive(dfs_dir_mtd); 2661 cleanup_mtdchar(); 2662 if (proc_mtd) 2663 remove_proc_entry("mtd", NULL); 2664 class_unregister(&mtd_class); 2665 bdi_unregister(mtd_bdi); 2666 bdi_put(mtd_bdi); 2667 idr_destroy(&mtd_idr); 2668 } 2669 2670 module_init(init_mtd); 2671 module_exit(cleanup_mtd); 2672 2673 MODULE_LICENSE("GPL"); 2674 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 2675 MODULE_DESCRIPTION("Core MTD registration and access routines"); 2676