1 /* 2 * Core registration and callback routines for MTD 3 * drivers and users. 4 * 5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> 6 * Copyright © 2006 Red Hat UK Limited 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 * 10 */ 11 12 #ifndef __UBOOT__ 13 #include <linux/module.h> 14 #include <linux/kernel.h> 15 #include <linux/ptrace.h> 16 #include <linux/seq_file.h> 17 #include <linux/string.h> 18 #include <linux/timer.h> 19 #include <linux/major.h> 20 #include <linux/fs.h> 21 #include <linux/err.h> 22 #include <linux/ioctl.h> 23 #include <linux/init.h> 24 #include <linux/proc_fs.h> 25 #include <linux/idr.h> 26 #include <linux/backing-dev.h> 27 #include <linux/gfp.h> 28 #include <linux/slab.h> 29 #else 30 #include <linux/err.h> 31 #include <ubi_uboot.h> 32 #endif 33 34 #include <linux/log2.h> 35 #include <linux/mtd/mtd.h> 36 #include <linux/mtd/partitions.h> 37 38 #include "mtdcore.h" 39 40 #ifndef __UBOOT__ 41 /* 42 * backing device capabilities for non-mappable devices (such as NAND flash) 43 * - permits private mappings, copies are taken of the data 44 */ 45 static struct backing_dev_info mtd_bdi_unmappable = { 46 .capabilities = BDI_CAP_MAP_COPY, 47 }; 48 49 /* 50 * backing device capabilities for R/O mappable devices (such as ROM) 51 * - permits private mappings, copies are taken of the data 52 * - permits non-writable shared mappings 53 */ 54 static struct backing_dev_info mtd_bdi_ro_mappable = { 55 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | 56 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP), 57 }; 58 59 /* 60 * backing device capabilities for writable mappable devices (such as RAM) 61 * - permits private mappings, copies are taken of the data 62 * - permits non-writable shared mappings 63 */ 64 static struct backing_dev_info mtd_bdi_rw_mappable = { 65 .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT | 66 BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP | 67 BDI_CAP_WRITE_MAP), 68 }; 69 70 static int mtd_cls_suspend(struct device *dev, pm_message_t state); 71 static int mtd_cls_resume(struct device *dev); 72 73 static struct class mtd_class = { 74 .name = "mtd", 75 .owner = THIS_MODULE, 76 .suspend = mtd_cls_suspend, 77 .resume = mtd_cls_resume, 78 }; 79 #else 80 struct mtd_info *mtd_table[MAX_MTD_DEVICES]; 81 82 #define MAX_IDR_ID 64 83 84 struct idr_layer { 85 int used; 86 void *ptr; 87 }; 88 89 struct idr { 90 struct idr_layer id[MAX_IDR_ID]; 91 }; 92 93 #define DEFINE_IDR(name) struct idr name; 94 95 void idr_remove(struct idr *idp, int id) 96 { 97 if (idp->id[id].used) 98 idp->id[id].used = 0; 99 100 return; 101 } 102 void *idr_find(struct idr *idp, int id) 103 { 104 if (idp->id[id].used) 105 return idp->id[id].ptr; 106 107 return NULL; 108 } 109 110 void *idr_get_next(struct idr *idp, int *next) 111 { 112 void *ret; 113 int id = *next; 114 115 ret = idr_find(idp, id); 116 if (ret) { 117 id ++; 118 if (!idp->id[id].used) 119 id = 0; 120 *next = id; 121 } else { 122 *next = 0; 123 } 124 125 return ret; 126 } 127 128 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask) 129 { 130 struct idr_layer *idl; 131 int i = 0; 132 133 while (i < MAX_IDR_ID) { 134 idl = &idp->id[i]; 135 if (idl->used == 0) { 136 idl->used = 1; 137 idl->ptr = ptr; 138 return i; 139 } 140 i++; 141 } 142 return -ENOSPC; 143 } 144 #endif 145 146 static DEFINE_IDR(mtd_idr); 147 148 /* These are exported solely for the purpose of mtd_blkdevs.c. You 149 should not use them for _anything_ else */ 150 DEFINE_MUTEX(mtd_table_mutex); 151 EXPORT_SYMBOL_GPL(mtd_table_mutex); 152 153 struct mtd_info *__mtd_next_device(int i) 154 { 155 return idr_get_next(&mtd_idr, &i); 156 } 157 EXPORT_SYMBOL_GPL(__mtd_next_device); 158 159 #ifndef __UBOOT__ 160 static LIST_HEAD(mtd_notifiers); 161 162 163 #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2) 164 165 /* REVISIT once MTD uses the driver model better, whoever allocates 166 * the mtd_info will probably want to use the release() hook... 167 */ 168 static void mtd_release(struct device *dev) 169 { 170 struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev); 171 dev_t index = MTD_DEVT(mtd->index); 172 173 /* remove /dev/mtdXro node if needed */ 174 if (index) 175 device_destroy(&mtd_class, index + 1); 176 } 177 178 static int mtd_cls_suspend(struct device *dev, pm_message_t state) 179 { 180 struct mtd_info *mtd = dev_get_drvdata(dev); 181 182 return mtd ? mtd_suspend(mtd) : 0; 183 } 184 185 static int mtd_cls_resume(struct device *dev) 186 { 187 struct mtd_info *mtd = dev_get_drvdata(dev); 188 189 if (mtd) 190 mtd_resume(mtd); 191 return 0; 192 } 193 194 static ssize_t mtd_type_show(struct device *dev, 195 struct device_attribute *attr, char *buf) 196 { 197 struct mtd_info *mtd = dev_get_drvdata(dev); 198 char *type; 199 200 switch (mtd->type) { 201 case MTD_ABSENT: 202 type = "absent"; 203 break; 204 case MTD_RAM: 205 type = "ram"; 206 break; 207 case MTD_ROM: 208 type = "rom"; 209 break; 210 case MTD_NORFLASH: 211 type = "nor"; 212 break; 213 case MTD_NANDFLASH: 214 type = "nand"; 215 break; 216 case MTD_DATAFLASH: 217 type = "dataflash"; 218 break; 219 case MTD_UBIVOLUME: 220 type = "ubi"; 221 break; 222 case MTD_MLCNANDFLASH: 223 type = "mlc-nand"; 224 break; 225 default: 226 type = "unknown"; 227 } 228 229 return snprintf(buf, PAGE_SIZE, "%s\n", type); 230 } 231 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); 232 233 static ssize_t mtd_flags_show(struct device *dev, 234 struct device_attribute *attr, char *buf) 235 { 236 struct mtd_info *mtd = dev_get_drvdata(dev); 237 238 return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); 239 240 } 241 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); 242 243 static ssize_t mtd_size_show(struct device *dev, 244 struct device_attribute *attr, char *buf) 245 { 246 struct mtd_info *mtd = dev_get_drvdata(dev); 247 248 return snprintf(buf, PAGE_SIZE, "%llu\n", 249 (unsigned long long)mtd->size); 250 251 } 252 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); 253 254 static ssize_t mtd_erasesize_show(struct device *dev, 255 struct device_attribute *attr, char *buf) 256 { 257 struct mtd_info *mtd = dev_get_drvdata(dev); 258 259 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); 260 261 } 262 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); 263 264 static ssize_t mtd_writesize_show(struct device *dev, 265 struct device_attribute *attr, char *buf) 266 { 267 struct mtd_info *mtd = dev_get_drvdata(dev); 268 269 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); 270 271 } 272 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); 273 274 static ssize_t mtd_subpagesize_show(struct device *dev, 275 struct device_attribute *attr, char *buf) 276 { 277 struct mtd_info *mtd = dev_get_drvdata(dev); 278 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; 279 280 return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); 281 282 } 283 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); 284 285 static ssize_t mtd_oobsize_show(struct device *dev, 286 struct device_attribute *attr, char *buf) 287 { 288 struct mtd_info *mtd = dev_get_drvdata(dev); 289 290 return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); 291 292 } 293 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); 294 295 static ssize_t mtd_numeraseregions_show(struct device *dev, 296 struct device_attribute *attr, char *buf) 297 { 298 struct mtd_info *mtd = dev_get_drvdata(dev); 299 300 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); 301 302 } 303 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, 304 NULL); 305 306 static ssize_t mtd_name_show(struct device *dev, 307 struct device_attribute *attr, char *buf) 308 { 309 struct mtd_info *mtd = dev_get_drvdata(dev); 310 311 return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); 312 313 } 314 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); 315 316 static ssize_t mtd_ecc_strength_show(struct device *dev, 317 struct device_attribute *attr, char *buf) 318 { 319 struct mtd_info *mtd = dev_get_drvdata(dev); 320 321 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); 322 } 323 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); 324 325 static ssize_t mtd_bitflip_threshold_show(struct device *dev, 326 struct device_attribute *attr, 327 char *buf) 328 { 329 struct mtd_info *mtd = dev_get_drvdata(dev); 330 331 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); 332 } 333 334 static ssize_t mtd_bitflip_threshold_store(struct device *dev, 335 struct device_attribute *attr, 336 const char *buf, size_t count) 337 { 338 struct mtd_info *mtd = dev_get_drvdata(dev); 339 unsigned int bitflip_threshold; 340 int retval; 341 342 retval = kstrtouint(buf, 0, &bitflip_threshold); 343 if (retval) 344 return retval; 345 346 mtd->bitflip_threshold = bitflip_threshold; 347 return count; 348 } 349 static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR, 350 mtd_bitflip_threshold_show, 351 mtd_bitflip_threshold_store); 352 353 static ssize_t mtd_ecc_step_size_show(struct device *dev, 354 struct device_attribute *attr, char *buf) 355 { 356 struct mtd_info *mtd = dev_get_drvdata(dev); 357 358 return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); 359 360 } 361 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); 362 363 static struct attribute *mtd_attrs[] = { 364 &dev_attr_type.attr, 365 &dev_attr_flags.attr, 366 &dev_attr_size.attr, 367 &dev_attr_erasesize.attr, 368 &dev_attr_writesize.attr, 369 &dev_attr_subpagesize.attr, 370 &dev_attr_oobsize.attr, 371 &dev_attr_numeraseregions.attr, 372 &dev_attr_name.attr, 373 &dev_attr_ecc_strength.attr, 374 &dev_attr_ecc_step_size.attr, 375 &dev_attr_bitflip_threshold.attr, 376 NULL, 377 }; 378 ATTRIBUTE_GROUPS(mtd); 379 380 static struct device_type mtd_devtype = { 381 .name = "mtd", 382 .groups = mtd_groups, 383 .release = mtd_release, 384 }; 385 #endif 386 387 /** 388 * add_mtd_device - register an MTD device 389 * @mtd: pointer to new MTD device info structure 390 * 391 * Add a device to the list of MTD devices present in the system, and 392 * notify each currently active MTD 'user' of its arrival. Returns 393 * zero on success or 1 on failure, which currently will only happen 394 * if there is insufficient memory or a sysfs error. 395 */ 396 397 int add_mtd_device(struct mtd_info *mtd) 398 { 399 #ifndef __UBOOT__ 400 struct mtd_notifier *not; 401 #endif 402 int i, error; 403 404 #ifndef __UBOOT__ 405 if (!mtd->backing_dev_info) { 406 switch (mtd->type) { 407 case MTD_RAM: 408 mtd->backing_dev_info = &mtd_bdi_rw_mappable; 409 break; 410 case MTD_ROM: 411 mtd->backing_dev_info = &mtd_bdi_ro_mappable; 412 break; 413 default: 414 mtd->backing_dev_info = &mtd_bdi_unmappable; 415 break; 416 } 417 } 418 #endif 419 420 BUG_ON(mtd->writesize == 0); 421 mutex_lock(&mtd_table_mutex); 422 423 i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL); 424 if (i < 0) 425 goto fail_locked; 426 427 mtd->index = i; 428 mtd->usecount = 0; 429 430 INIT_LIST_HEAD(&mtd->partitions); 431 432 /* default value if not set by driver */ 433 if (mtd->bitflip_threshold == 0) 434 mtd->bitflip_threshold = mtd->ecc_strength; 435 436 if (is_power_of_2(mtd->erasesize)) 437 mtd->erasesize_shift = ffs(mtd->erasesize) - 1; 438 else 439 mtd->erasesize_shift = 0; 440 441 if (is_power_of_2(mtd->writesize)) 442 mtd->writesize_shift = ffs(mtd->writesize) - 1; 443 else 444 mtd->writesize_shift = 0; 445 446 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; 447 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; 448 449 /* Some chips always power up locked. Unlock them now */ 450 if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) { 451 error = mtd_unlock(mtd, 0, mtd->size); 452 if (error && error != -EOPNOTSUPP) 453 printk(KERN_WARNING 454 "%s: unlock failed, writes may not work\n", 455 mtd->name); 456 } 457 458 #ifndef __UBOOT__ 459 /* Caller should have set dev.parent to match the 460 * physical device. 461 */ 462 mtd->dev.type = &mtd_devtype; 463 mtd->dev.class = &mtd_class; 464 mtd->dev.devt = MTD_DEVT(i); 465 dev_set_name(&mtd->dev, "mtd%d", i); 466 dev_set_drvdata(&mtd->dev, mtd); 467 if (device_register(&mtd->dev) != 0) 468 goto fail_added; 469 470 if (MTD_DEVT(i)) 471 device_create(&mtd_class, mtd->dev.parent, 472 MTD_DEVT(i) + 1, 473 NULL, "mtd%dro", i); 474 475 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 476 /* No need to get a refcount on the module containing 477 the notifier, since we hold the mtd_table_mutex */ 478 list_for_each_entry(not, &mtd_notifiers, list) 479 not->add(mtd); 480 #else 481 pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name); 482 #endif 483 484 mutex_unlock(&mtd_table_mutex); 485 /* We _know_ we aren't being removed, because 486 our caller is still holding us here. So none 487 of this try_ nonsense, and no bitching about it 488 either. :) */ 489 __module_get(THIS_MODULE); 490 return 0; 491 492 #ifndef __UBOOT__ 493 fail_added: 494 idr_remove(&mtd_idr, i); 495 #endif 496 fail_locked: 497 mutex_unlock(&mtd_table_mutex); 498 return 1; 499 } 500 501 /** 502 * del_mtd_device - unregister an MTD device 503 * @mtd: pointer to MTD device info structure 504 * 505 * Remove a device from the list of MTD devices present in the system, 506 * and notify each currently active MTD 'user' of its departure. 507 * Returns zero on success or 1 on failure, which currently will happen 508 * if the requested device does not appear to be present in the list. 509 */ 510 511 int del_mtd_device(struct mtd_info *mtd) 512 { 513 int ret; 514 #ifndef __UBOOT__ 515 struct mtd_notifier *not; 516 #endif 517 518 mutex_lock(&mtd_table_mutex); 519 520 if (idr_find(&mtd_idr, mtd->index) != mtd) { 521 ret = -ENODEV; 522 goto out_error; 523 } 524 525 #ifndef __UBOOT__ 526 /* No need to get a refcount on the module containing 527 the notifier, since we hold the mtd_table_mutex */ 528 list_for_each_entry(not, &mtd_notifiers, list) 529 not->remove(mtd); 530 #endif 531 532 if (mtd->usecount) { 533 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", 534 mtd->index, mtd->name, mtd->usecount); 535 ret = -EBUSY; 536 } else { 537 #ifndef __UBOOT__ 538 device_unregister(&mtd->dev); 539 #endif 540 541 idr_remove(&mtd_idr, mtd->index); 542 543 module_put(THIS_MODULE); 544 ret = 0; 545 } 546 547 out_error: 548 mutex_unlock(&mtd_table_mutex); 549 return ret; 550 } 551 552 #ifndef __UBOOT__ 553 /** 554 * mtd_device_parse_register - parse partitions and register an MTD device. 555 * 556 * @mtd: the MTD device to register 557 * @types: the list of MTD partition probes to try, see 558 * 'parse_mtd_partitions()' for more information 559 * @parser_data: MTD partition parser-specific data 560 * @parts: fallback partition information to register, if parsing fails; 561 * only valid if %nr_parts > %0 562 * @nr_parts: the number of partitions in parts, if zero then the full 563 * MTD device is registered if no partition info is found 564 * 565 * This function aggregates MTD partitions parsing (done by 566 * 'parse_mtd_partitions()') and MTD device and partitions registering. It 567 * basically follows the most common pattern found in many MTD drivers: 568 * 569 * * It first tries to probe partitions on MTD device @mtd using parsers 570 * specified in @types (if @types is %NULL, then the default list of parsers 571 * is used, see 'parse_mtd_partitions()' for more information). If none are 572 * found this functions tries to fallback to information specified in 573 * @parts/@nr_parts. 574 * * If any partitioning info was found, this function registers the found 575 * partitions. 576 * * If no partitions were found this function just registers the MTD device 577 * @mtd and exits. 578 * 579 * Returns zero in case of success and a negative error code in case of failure. 580 */ 581 int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types, 582 struct mtd_part_parser_data *parser_data, 583 const struct mtd_partition *parts, 584 int nr_parts) 585 { 586 int err; 587 struct mtd_partition *real_parts; 588 589 err = parse_mtd_partitions(mtd, types, &real_parts, parser_data); 590 if (err <= 0 && nr_parts && parts) { 591 real_parts = kmemdup(parts, sizeof(*parts) * nr_parts, 592 GFP_KERNEL); 593 if (!real_parts) 594 err = -ENOMEM; 595 else 596 err = nr_parts; 597 } 598 599 if (err > 0) { 600 err = add_mtd_partitions(mtd, real_parts, err); 601 kfree(real_parts); 602 } else if (err == 0) { 603 err = add_mtd_device(mtd); 604 if (err == 1) 605 err = -ENODEV; 606 } 607 608 return err; 609 } 610 EXPORT_SYMBOL_GPL(mtd_device_parse_register); 611 612 /** 613 * mtd_device_unregister - unregister an existing MTD device. 614 * 615 * @master: the MTD device to unregister. This will unregister both the master 616 * and any partitions if registered. 617 */ 618 int mtd_device_unregister(struct mtd_info *master) 619 { 620 int err; 621 622 err = del_mtd_partitions(master); 623 if (err) 624 return err; 625 626 if (!device_is_registered(&master->dev)) 627 return 0; 628 629 return del_mtd_device(master); 630 } 631 EXPORT_SYMBOL_GPL(mtd_device_unregister); 632 633 /** 634 * register_mtd_user - register a 'user' of MTD devices. 635 * @new: pointer to notifier info structure 636 * 637 * Registers a pair of callbacks function to be called upon addition 638 * or removal of MTD devices. Causes the 'add' callback to be immediately 639 * invoked for each MTD device currently present in the system. 640 */ 641 void register_mtd_user (struct mtd_notifier *new) 642 { 643 struct mtd_info *mtd; 644 645 mutex_lock(&mtd_table_mutex); 646 647 list_add(&new->list, &mtd_notifiers); 648 649 __module_get(THIS_MODULE); 650 651 mtd_for_each_device(mtd) 652 new->add(mtd); 653 654 mutex_unlock(&mtd_table_mutex); 655 } 656 EXPORT_SYMBOL_GPL(register_mtd_user); 657 658 /** 659 * unregister_mtd_user - unregister a 'user' of MTD devices. 660 * @old: pointer to notifier info structure 661 * 662 * Removes a callback function pair from the list of 'users' to be 663 * notified upon addition or removal of MTD devices. Causes the 664 * 'remove' callback to be immediately invoked for each MTD device 665 * currently present in the system. 666 */ 667 int unregister_mtd_user (struct mtd_notifier *old) 668 { 669 struct mtd_info *mtd; 670 671 mutex_lock(&mtd_table_mutex); 672 673 module_put(THIS_MODULE); 674 675 mtd_for_each_device(mtd) 676 old->remove(mtd); 677 678 list_del(&old->list); 679 mutex_unlock(&mtd_table_mutex); 680 return 0; 681 } 682 EXPORT_SYMBOL_GPL(unregister_mtd_user); 683 #endif 684 685 /** 686 * get_mtd_device - obtain a validated handle for an MTD device 687 * @mtd: last known address of the required MTD device 688 * @num: internal device number of the required MTD device 689 * 690 * Given a number and NULL address, return the num'th entry in the device 691 * table, if any. Given an address and num == -1, search the device table 692 * for a device with that address and return if it's still present. Given 693 * both, return the num'th driver only if its address matches. Return 694 * error code if not. 695 */ 696 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 697 { 698 struct mtd_info *ret = NULL, *other; 699 int err = -ENODEV; 700 701 mutex_lock(&mtd_table_mutex); 702 703 if (num == -1) { 704 mtd_for_each_device(other) { 705 if (other == mtd) { 706 ret = mtd; 707 break; 708 } 709 } 710 } else if (num >= 0) { 711 ret = idr_find(&mtd_idr, num); 712 if (mtd && mtd != ret) 713 ret = NULL; 714 } 715 716 if (!ret) { 717 ret = ERR_PTR(err); 718 goto out; 719 } 720 721 err = __get_mtd_device(ret); 722 if (err) 723 ret = ERR_PTR(err); 724 out: 725 mutex_unlock(&mtd_table_mutex); 726 return ret; 727 } 728 EXPORT_SYMBOL_GPL(get_mtd_device); 729 730 731 int __get_mtd_device(struct mtd_info *mtd) 732 { 733 int err; 734 735 if (!try_module_get(mtd->owner)) 736 return -ENODEV; 737 738 if (mtd->_get_device) { 739 err = mtd->_get_device(mtd); 740 741 if (err) { 742 module_put(mtd->owner); 743 return err; 744 } 745 } 746 mtd->usecount++; 747 return 0; 748 } 749 EXPORT_SYMBOL_GPL(__get_mtd_device); 750 751 /** 752 * get_mtd_device_nm - obtain a validated handle for an MTD device by 753 * device name 754 * @name: MTD device name to open 755 * 756 * This function returns MTD device description structure in case of 757 * success and an error code in case of failure. 758 */ 759 struct mtd_info *get_mtd_device_nm(const char *name) 760 { 761 int err = -ENODEV; 762 struct mtd_info *mtd = NULL, *other; 763 764 mutex_lock(&mtd_table_mutex); 765 766 mtd_for_each_device(other) { 767 if (!strcmp(name, other->name)) { 768 mtd = other; 769 break; 770 } 771 } 772 773 if (!mtd) 774 goto out_unlock; 775 776 err = __get_mtd_device(mtd); 777 if (err) 778 goto out_unlock; 779 780 mutex_unlock(&mtd_table_mutex); 781 return mtd; 782 783 out_unlock: 784 mutex_unlock(&mtd_table_mutex); 785 return ERR_PTR(err); 786 } 787 EXPORT_SYMBOL_GPL(get_mtd_device_nm); 788 789 #if defined(CONFIG_CMD_MTDPARTS_SPREAD) 790 /** 791 * mtd_get_len_incl_bad 792 * 793 * Check if length including bad blocks fits into device. 794 * 795 * @param mtd an MTD device 796 * @param offset offset in flash 797 * @param length image length 798 * @return image length including bad blocks in *len_incl_bad and whether or not 799 * the length returned was truncated in *truncated 800 */ 801 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset, 802 const uint64_t length, uint64_t *len_incl_bad, 803 int *truncated) 804 { 805 *truncated = 0; 806 *len_incl_bad = 0; 807 808 if (!mtd->_block_isbad) { 809 *len_incl_bad = length; 810 return; 811 } 812 813 uint64_t len_excl_bad = 0; 814 uint64_t block_len; 815 816 while (len_excl_bad < length) { 817 if (offset >= mtd->size) { 818 *truncated = 1; 819 return; 820 } 821 822 block_len = mtd->erasesize - (offset & (mtd->erasesize - 1)); 823 824 if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1))) 825 len_excl_bad += block_len; 826 827 *len_incl_bad += block_len; 828 offset += block_len; 829 } 830 } 831 #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */ 832 833 void put_mtd_device(struct mtd_info *mtd) 834 { 835 mutex_lock(&mtd_table_mutex); 836 __put_mtd_device(mtd); 837 mutex_unlock(&mtd_table_mutex); 838 839 } 840 EXPORT_SYMBOL_GPL(put_mtd_device); 841 842 void __put_mtd_device(struct mtd_info *mtd) 843 { 844 --mtd->usecount; 845 BUG_ON(mtd->usecount < 0); 846 847 if (mtd->_put_device) 848 mtd->_put_device(mtd); 849 850 module_put(mtd->owner); 851 } 852 EXPORT_SYMBOL_GPL(__put_mtd_device); 853 854 /* 855 * Erase is an asynchronous operation. Device drivers are supposed 856 * to call instr->callback() whenever the operation completes, even 857 * if it completes with a failure. 858 * Callers are supposed to pass a callback function and wait for it 859 * to be called before writing to the block. 860 */ 861 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 862 { 863 if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr) 864 return -EINVAL; 865 if (!(mtd->flags & MTD_WRITEABLE)) 866 return -EROFS; 867 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 868 if (!instr->len) { 869 instr->state = MTD_ERASE_DONE; 870 mtd_erase_callback(instr); 871 return 0; 872 } 873 return mtd->_erase(mtd, instr); 874 } 875 EXPORT_SYMBOL_GPL(mtd_erase); 876 877 #ifndef __UBOOT__ 878 /* 879 * This stuff for eXecute-In-Place. phys is optional and may be set to NULL. 880 */ 881 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 882 void **virt, resource_size_t *phys) 883 { 884 *retlen = 0; 885 *virt = NULL; 886 if (phys) 887 *phys = 0; 888 if (!mtd->_point) 889 return -EOPNOTSUPP; 890 if (from < 0 || from > mtd->size || len > mtd->size - from) 891 return -EINVAL; 892 if (!len) 893 return 0; 894 return mtd->_point(mtd, from, len, retlen, virt, phys); 895 } 896 EXPORT_SYMBOL_GPL(mtd_point); 897 898 /* We probably shouldn't allow XIP if the unpoint isn't a NULL */ 899 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 900 { 901 if (!mtd->_point) 902 return -EOPNOTSUPP; 903 if (from < 0 || from > mtd->size || len > mtd->size - from) 904 return -EINVAL; 905 if (!len) 906 return 0; 907 return mtd->_unpoint(mtd, from, len); 908 } 909 EXPORT_SYMBOL_GPL(mtd_unpoint); 910 #endif 911 912 /* 913 * Allow NOMMU mmap() to directly map the device (if not NULL) 914 * - return the address to which the offset maps 915 * - return -ENOSYS to indicate refusal to do the mapping 916 */ 917 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len, 918 unsigned long offset, unsigned long flags) 919 { 920 if (!mtd->_get_unmapped_area) 921 return -EOPNOTSUPP; 922 if (offset > mtd->size || len > mtd->size - offset) 923 return -EINVAL; 924 return mtd->_get_unmapped_area(mtd, len, offset, flags); 925 } 926 EXPORT_SYMBOL_GPL(mtd_get_unmapped_area); 927 928 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, 929 u_char *buf) 930 { 931 int ret_code; 932 *retlen = 0; 933 if (from < 0 || from > mtd->size || len > mtd->size - from) 934 return -EINVAL; 935 if (!len) 936 return 0; 937 938 /* 939 * In the absence of an error, drivers return a non-negative integer 940 * representing the maximum number of bitflips that were corrected on 941 * any one ecc region (if applicable; zero otherwise). 942 */ 943 if (mtd->_read) { 944 ret_code = mtd->_read(mtd, from, len, retlen, buf); 945 } else if (mtd->_read_oob) { 946 struct mtd_oob_ops ops = { 947 .len = len, 948 .datbuf = buf, 949 }; 950 951 ret_code = mtd->_read_oob(mtd, from, &ops); 952 *retlen = ops.retlen; 953 } else { 954 return -ENOTSUPP; 955 } 956 957 if (unlikely(ret_code < 0)) 958 return ret_code; 959 if (mtd->ecc_strength == 0) 960 return 0; /* device lacks ecc */ 961 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 962 } 963 EXPORT_SYMBOL_GPL(mtd_read); 964 965 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 966 const u_char *buf) 967 { 968 *retlen = 0; 969 if (to < 0 || to > mtd->size || len > mtd->size - to) 970 return -EINVAL; 971 if ((!mtd->_write && !mtd->_write_oob) || 972 !(mtd->flags & MTD_WRITEABLE)) 973 return -EROFS; 974 if (!len) 975 return 0; 976 977 if (!mtd->_write) { 978 struct mtd_oob_ops ops = { 979 .len = len, 980 .datbuf = (u8 *)buf, 981 }; 982 int ret; 983 984 ret = mtd->_write_oob(mtd, to, &ops); 985 *retlen = ops.retlen; 986 return ret; 987 } 988 989 return mtd->_write(mtd, to, len, retlen, buf); 990 } 991 EXPORT_SYMBOL_GPL(mtd_write); 992 993 /* 994 * In blackbox flight recorder like scenarios we want to make successful writes 995 * in interrupt context. panic_write() is only intended to be called when its 996 * known the kernel is about to panic and we need the write to succeed. Since 997 * the kernel is not going to be running for much longer, this function can 998 * break locks and delay to ensure the write succeeds (but not sleep). 999 */ 1000 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, 1001 const u_char *buf) 1002 { 1003 *retlen = 0; 1004 if (!mtd->_panic_write) 1005 return -EOPNOTSUPP; 1006 if (to < 0 || to > mtd->size || len > mtd->size - to) 1007 return -EINVAL; 1008 if (!(mtd->flags & MTD_WRITEABLE)) 1009 return -EROFS; 1010 if (!len) 1011 return 0; 1012 return mtd->_panic_write(mtd, to, len, retlen, buf); 1013 } 1014 EXPORT_SYMBOL_GPL(mtd_panic_write); 1015 1016 static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs, 1017 struct mtd_oob_ops *ops) 1018 { 1019 /* 1020 * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving 1021 * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in 1022 * this case. 1023 */ 1024 if (!ops->datbuf) 1025 ops->len = 0; 1026 1027 if (!ops->oobbuf) 1028 ops->ooblen = 0; 1029 1030 if (offs < 0 || offs + ops->len > mtd->size) 1031 return -EINVAL; 1032 1033 if (ops->ooblen) { 1034 u64 maxooblen; 1035 1036 if (ops->ooboffs >= mtd_oobavail(mtd, ops)) 1037 return -EINVAL; 1038 1039 maxooblen = ((mtd_div_by_ws(mtd->size, mtd) - 1040 mtd_div_by_ws(offs, mtd)) * 1041 mtd_oobavail(mtd, ops)) - ops->ooboffs; 1042 if (ops->ooblen > maxooblen) 1043 return -EINVAL; 1044 } 1045 1046 return 0; 1047 } 1048 1049 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) 1050 { 1051 int ret_code; 1052 ops->retlen = ops->oobretlen = 0; 1053 1054 ret_code = mtd_check_oob_ops(mtd, from, ops); 1055 if (ret_code) 1056 return ret_code; 1057 1058 /* Check the validity of a potential fallback on mtd->_read */ 1059 if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf)) 1060 return -EOPNOTSUPP; 1061 1062 if (mtd->_read_oob) 1063 ret_code = mtd->_read_oob(mtd, from, ops); 1064 else 1065 ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen, 1066 ops->datbuf); 1067 1068 /* 1069 * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics 1070 * similar to mtd->_read(), returning a non-negative integer 1071 * representing max bitflips. In other cases, mtd->_read_oob() may 1072 * return -EUCLEAN. In all cases, perform similar logic to mtd_read(). 1073 */ 1074 if (unlikely(ret_code < 0)) 1075 return ret_code; 1076 if (mtd->ecc_strength == 0) 1077 return 0; /* device lacks ecc */ 1078 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0; 1079 } 1080 EXPORT_SYMBOL_GPL(mtd_read_oob); 1081 1082 int mtd_write_oob(struct mtd_info *mtd, loff_t to, 1083 struct mtd_oob_ops *ops) 1084 { 1085 int ret; 1086 1087 ops->retlen = ops->oobretlen = 0; 1088 1089 if (!(mtd->flags & MTD_WRITEABLE)) 1090 return -EROFS; 1091 1092 ret = mtd_check_oob_ops(mtd, to, ops); 1093 if (ret) 1094 return ret; 1095 1096 /* Check the validity of a potential fallback on mtd->_write */ 1097 if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf)) 1098 return -EOPNOTSUPP; 1099 1100 if (mtd->_write_oob) 1101 return mtd->_write_oob(mtd, to, ops); 1102 else 1103 return mtd->_write(mtd, to, ops->len, &ops->retlen, 1104 ops->datbuf); 1105 } 1106 EXPORT_SYMBOL_GPL(mtd_write_oob); 1107 1108 /** 1109 * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section 1110 * @mtd: MTD device structure 1111 * @section: ECC section. Depending on the layout you may have all the ECC 1112 * bytes stored in a single contiguous section, or one section 1113 * per ECC chunk (and sometime several sections for a single ECC 1114 * ECC chunk) 1115 * @oobecc: OOB region struct filled with the appropriate ECC position 1116 * information 1117 * 1118 * This function returns ECC section information in the OOB area. If you want 1119 * to get all the ECC bytes information, then you should call 1120 * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE. 1121 * 1122 * Returns zero on success, a negative error code otherwise. 1123 */ 1124 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, 1125 struct mtd_oob_region *oobecc) 1126 { 1127 memset(oobecc, 0, sizeof(*oobecc)); 1128 1129 if (!mtd || section < 0) 1130 return -EINVAL; 1131 1132 if (!mtd->ooblayout || !mtd->ooblayout->ecc) 1133 return -ENOTSUPP; 1134 1135 return mtd->ooblayout->ecc(mtd, section, oobecc); 1136 } 1137 EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc); 1138 1139 /** 1140 * mtd_ooblayout_free - Get the OOB region definition of a specific free 1141 * section 1142 * @mtd: MTD device structure 1143 * @section: Free section you are interested in. Depending on the layout 1144 * you may have all the free bytes stored in a single contiguous 1145 * section, or one section per ECC chunk plus an extra section 1146 * for the remaining bytes (or other funky layout). 1147 * @oobfree: OOB region struct filled with the appropriate free position 1148 * information 1149 * 1150 * This function returns free bytes position in the OOB area. If you want 1151 * to get all the free bytes information, then you should call 1152 * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE. 1153 * 1154 * Returns zero on success, a negative error code otherwise. 1155 */ 1156 int mtd_ooblayout_free(struct mtd_info *mtd, int section, 1157 struct mtd_oob_region *oobfree) 1158 { 1159 memset(oobfree, 0, sizeof(*oobfree)); 1160 1161 if (!mtd || section < 0) 1162 return -EINVAL; 1163 1164 if (!mtd->ooblayout || !mtd->ooblayout->free) 1165 return -ENOTSUPP; 1166 1167 return mtd->ooblayout->free(mtd, section, oobfree); 1168 } 1169 EXPORT_SYMBOL_GPL(mtd_ooblayout_free); 1170 1171 /** 1172 * mtd_ooblayout_find_region - Find the region attached to a specific byte 1173 * @mtd: mtd info structure 1174 * @byte: the byte we are searching for 1175 * @sectionp: pointer where the section id will be stored 1176 * @oobregion: used to retrieve the ECC position 1177 * @iter: iterator function. Should be either mtd_ooblayout_free or 1178 * mtd_ooblayout_ecc depending on the region type you're searching for 1179 * 1180 * This function returns the section id and oobregion information of a 1181 * specific byte. For example, say you want to know where the 4th ECC byte is 1182 * stored, you'll use: 1183 * 1184 * mtd_ooblayout_find_region(mtd, 3, §ion, &oobregion, mtd_ooblayout_ecc); 1185 * 1186 * Returns zero on success, a negative error code otherwise. 1187 */ 1188 static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte, 1189 int *sectionp, struct mtd_oob_region *oobregion, 1190 int (*iter)(struct mtd_info *, 1191 int section, 1192 struct mtd_oob_region *oobregion)) 1193 { 1194 int pos = 0, ret, section = 0; 1195 1196 memset(oobregion, 0, sizeof(*oobregion)); 1197 1198 while (1) { 1199 ret = iter(mtd, section, oobregion); 1200 if (ret) 1201 return ret; 1202 1203 if (pos + oobregion->length > byte) 1204 break; 1205 1206 pos += oobregion->length; 1207 section++; 1208 } 1209 1210 /* 1211 * Adjust region info to make it start at the beginning at the 1212 * 'start' ECC byte. 1213 */ 1214 oobregion->offset += byte - pos; 1215 oobregion->length -= byte - pos; 1216 *sectionp = section; 1217 1218 return 0; 1219 } 1220 1221 /** 1222 * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific 1223 * ECC byte 1224 * @mtd: mtd info structure 1225 * @eccbyte: the byte we are searching for 1226 * @sectionp: pointer where the section id will be stored 1227 * @oobregion: OOB region information 1228 * 1229 * Works like mtd_ooblayout_find_region() except it searches for a specific ECC 1230 * byte. 1231 * 1232 * Returns zero on success, a negative error code otherwise. 1233 */ 1234 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte, 1235 int *section, 1236 struct mtd_oob_region *oobregion) 1237 { 1238 return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion, 1239 mtd_ooblayout_ecc); 1240 } 1241 EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion); 1242 1243 /** 1244 * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer 1245 * @mtd: mtd info structure 1246 * @buf: destination buffer to store OOB bytes 1247 * @oobbuf: OOB buffer 1248 * @start: first byte to retrieve 1249 * @nbytes: number of bytes to retrieve 1250 * @iter: section iterator 1251 * 1252 * Extract bytes attached to a specific category (ECC or free) 1253 * from the OOB buffer and copy them into buf. 1254 * 1255 * Returns zero on success, a negative error code otherwise. 1256 */ 1257 static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf, 1258 const u8 *oobbuf, int start, int nbytes, 1259 int (*iter)(struct mtd_info *, 1260 int section, 1261 struct mtd_oob_region *oobregion)) 1262 { 1263 struct mtd_oob_region oobregion; 1264 int section, ret; 1265 1266 ret = mtd_ooblayout_find_region(mtd, start, §ion, 1267 &oobregion, iter); 1268 1269 while (!ret) { 1270 int cnt; 1271 1272 cnt = min_t(int, nbytes, oobregion.length); 1273 memcpy(buf, oobbuf + oobregion.offset, cnt); 1274 buf += cnt; 1275 nbytes -= cnt; 1276 1277 if (!nbytes) 1278 break; 1279 1280 ret = iter(mtd, ++section, &oobregion); 1281 } 1282 1283 return ret; 1284 } 1285 1286 /** 1287 * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer 1288 * @mtd: mtd info structure 1289 * @buf: source buffer to get OOB bytes from 1290 * @oobbuf: OOB buffer 1291 * @start: first OOB byte to set 1292 * @nbytes: number of OOB bytes to set 1293 * @iter: section iterator 1294 * 1295 * Fill the OOB buffer with data provided in buf. The category (ECC or free) 1296 * is selected by passing the appropriate iterator. 1297 * 1298 * Returns zero on success, a negative error code otherwise. 1299 */ 1300 static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf, 1301 u8 *oobbuf, int start, int nbytes, 1302 int (*iter)(struct mtd_info *, 1303 int section, 1304 struct mtd_oob_region *oobregion)) 1305 { 1306 struct mtd_oob_region oobregion; 1307 int section, ret; 1308 1309 ret = mtd_ooblayout_find_region(mtd, start, §ion, 1310 &oobregion, iter); 1311 1312 while (!ret) { 1313 int cnt; 1314 1315 cnt = min_t(int, nbytes, oobregion.length); 1316 memcpy(oobbuf + oobregion.offset, buf, cnt); 1317 buf += cnt; 1318 nbytes -= cnt; 1319 1320 if (!nbytes) 1321 break; 1322 1323 ret = iter(mtd, ++section, &oobregion); 1324 } 1325 1326 return ret; 1327 } 1328 1329 /** 1330 * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category 1331 * @mtd: mtd info structure 1332 * @iter: category iterator 1333 * 1334 * Count the number of bytes in a given category. 1335 * 1336 * Returns a positive value on success, a negative error code otherwise. 1337 */ 1338 static int mtd_ooblayout_count_bytes(struct mtd_info *mtd, 1339 int (*iter)(struct mtd_info *, 1340 int section, 1341 struct mtd_oob_region *oobregion)) 1342 { 1343 struct mtd_oob_region oobregion; 1344 int section = 0, ret, nbytes = 0; 1345 1346 while (1) { 1347 ret = iter(mtd, section++, &oobregion); 1348 if (ret) { 1349 if (ret == -ERANGE) 1350 ret = nbytes; 1351 break; 1352 } 1353 1354 nbytes += oobregion.length; 1355 } 1356 1357 return ret; 1358 } 1359 1360 /** 1361 * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer 1362 * @mtd: mtd info structure 1363 * @eccbuf: destination buffer to store ECC bytes 1364 * @oobbuf: OOB buffer 1365 * @start: first ECC byte to retrieve 1366 * @nbytes: number of ECC bytes to retrieve 1367 * 1368 * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes. 1369 * 1370 * Returns zero on success, a negative error code otherwise. 1371 */ 1372 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf, 1373 const u8 *oobbuf, int start, int nbytes) 1374 { 1375 return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes, 1376 mtd_ooblayout_ecc); 1377 } 1378 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes); 1379 1380 /** 1381 * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer 1382 * @mtd: mtd info structure 1383 * @eccbuf: source buffer to get ECC bytes from 1384 * @oobbuf: OOB buffer 1385 * @start: first ECC byte to set 1386 * @nbytes: number of ECC bytes to set 1387 * 1388 * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes. 1389 * 1390 * Returns zero on success, a negative error code otherwise. 1391 */ 1392 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf, 1393 u8 *oobbuf, int start, int nbytes) 1394 { 1395 return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes, 1396 mtd_ooblayout_ecc); 1397 } 1398 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes); 1399 1400 /** 1401 * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer 1402 * @mtd: mtd info structure 1403 * @databuf: destination buffer to store ECC bytes 1404 * @oobbuf: OOB buffer 1405 * @start: first ECC byte to retrieve 1406 * @nbytes: number of ECC bytes to retrieve 1407 * 1408 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. 1409 * 1410 * Returns zero on success, a negative error code otherwise. 1411 */ 1412 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf, 1413 const u8 *oobbuf, int start, int nbytes) 1414 { 1415 return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes, 1416 mtd_ooblayout_free); 1417 } 1418 EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes); 1419 1420 /** 1421 * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer 1422 * @mtd: mtd info structure 1423 * @eccbuf: source buffer to get data bytes from 1424 * @oobbuf: OOB buffer 1425 * @start: first ECC byte to set 1426 * @nbytes: number of ECC bytes to set 1427 * 1428 * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes. 1429 * 1430 * Returns zero on success, a negative error code otherwise. 1431 */ 1432 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf, 1433 u8 *oobbuf, int start, int nbytes) 1434 { 1435 return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes, 1436 mtd_ooblayout_free); 1437 } 1438 EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes); 1439 1440 /** 1441 * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB 1442 * @mtd: mtd info structure 1443 * 1444 * Works like mtd_ooblayout_count_bytes(), except it count free bytes. 1445 * 1446 * Returns zero on success, a negative error code otherwise. 1447 */ 1448 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd) 1449 { 1450 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free); 1451 } 1452 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes); 1453 1454 /** 1455 * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB 1456 * @mtd: mtd info structure 1457 * 1458 * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes. 1459 * 1460 * Returns zero on success, a negative error code otherwise. 1461 */ 1462 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd) 1463 { 1464 return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc); 1465 } 1466 EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes); 1467 1468 /* 1469 * Method to access the protection register area, present in some flash 1470 * devices. The user data is one time programmable but the factory data is read 1471 * only. 1472 */ 1473 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 1474 struct otp_info *buf) 1475 { 1476 if (!mtd->_get_fact_prot_info) 1477 return -EOPNOTSUPP; 1478 if (!len) 1479 return 0; 1480 return mtd->_get_fact_prot_info(mtd, len, retlen, buf); 1481 } 1482 EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info); 1483 1484 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 1485 size_t *retlen, u_char *buf) 1486 { 1487 *retlen = 0; 1488 if (!mtd->_read_fact_prot_reg) 1489 return -EOPNOTSUPP; 1490 if (!len) 1491 return 0; 1492 return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf); 1493 } 1494 EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg); 1495 1496 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen, 1497 struct otp_info *buf) 1498 { 1499 if (!mtd->_get_user_prot_info) 1500 return -EOPNOTSUPP; 1501 if (!len) 1502 return 0; 1503 return mtd->_get_user_prot_info(mtd, len, retlen, buf); 1504 } 1505 EXPORT_SYMBOL_GPL(mtd_get_user_prot_info); 1506 1507 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len, 1508 size_t *retlen, u_char *buf) 1509 { 1510 *retlen = 0; 1511 if (!mtd->_read_user_prot_reg) 1512 return -EOPNOTSUPP; 1513 if (!len) 1514 return 0; 1515 return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf); 1516 } 1517 EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg); 1518 1519 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len, 1520 size_t *retlen, u_char *buf) 1521 { 1522 int ret; 1523 1524 *retlen = 0; 1525 if (!mtd->_write_user_prot_reg) 1526 return -EOPNOTSUPP; 1527 if (!len) 1528 return 0; 1529 ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf); 1530 if (ret) 1531 return ret; 1532 1533 /* 1534 * If no data could be written at all, we are out of memory and 1535 * must return -ENOSPC. 1536 */ 1537 return (*retlen) ? 0 : -ENOSPC; 1538 } 1539 EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg); 1540 1541 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len) 1542 { 1543 if (!mtd->_lock_user_prot_reg) 1544 return -EOPNOTSUPP; 1545 if (!len) 1546 return 0; 1547 return mtd->_lock_user_prot_reg(mtd, from, len); 1548 } 1549 EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg); 1550 1551 /* Chip-supported device locking */ 1552 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1553 { 1554 if (!mtd->_lock) 1555 return -EOPNOTSUPP; 1556 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1557 return -EINVAL; 1558 if (!len) 1559 return 0; 1560 return mtd->_lock(mtd, ofs, len); 1561 } 1562 EXPORT_SYMBOL_GPL(mtd_lock); 1563 1564 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1565 { 1566 if (!mtd->_unlock) 1567 return -EOPNOTSUPP; 1568 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1569 return -EINVAL; 1570 if (!len) 1571 return 0; 1572 return mtd->_unlock(mtd, ofs, len); 1573 } 1574 EXPORT_SYMBOL_GPL(mtd_unlock); 1575 1576 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 1577 { 1578 if (!mtd->_is_locked) 1579 return -EOPNOTSUPP; 1580 if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) 1581 return -EINVAL; 1582 if (!len) 1583 return 0; 1584 return mtd->_is_locked(mtd, ofs, len); 1585 } 1586 EXPORT_SYMBOL_GPL(mtd_is_locked); 1587 1588 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs) 1589 { 1590 if (ofs < 0 || ofs > mtd->size) 1591 return -EINVAL; 1592 if (!mtd->_block_isreserved) 1593 return 0; 1594 return mtd->_block_isreserved(mtd, ofs); 1595 } 1596 EXPORT_SYMBOL_GPL(mtd_block_isreserved); 1597 1598 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs) 1599 { 1600 if (ofs < 0 || ofs > mtd->size) 1601 return -EINVAL; 1602 if (!mtd->_block_isbad) 1603 return 0; 1604 return mtd->_block_isbad(mtd, ofs); 1605 } 1606 EXPORT_SYMBOL_GPL(mtd_block_isbad); 1607 1608 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs) 1609 { 1610 if (!mtd->_block_markbad) 1611 return -EOPNOTSUPP; 1612 if (ofs < 0 || ofs > mtd->size) 1613 return -EINVAL; 1614 if (!(mtd->flags & MTD_WRITEABLE)) 1615 return -EROFS; 1616 return mtd->_block_markbad(mtd, ofs); 1617 } 1618 EXPORT_SYMBOL_GPL(mtd_block_markbad); 1619 1620 #ifndef __UBOOT__ 1621 /* 1622 * default_mtd_writev - the default writev method 1623 * @mtd: mtd device description object pointer 1624 * @vecs: the vectors to write 1625 * @count: count of vectors in @vecs 1626 * @to: the MTD device offset to write to 1627 * @retlen: on exit contains the count of bytes written to the MTD device. 1628 * 1629 * This function returns zero in case of success and a negative error code in 1630 * case of failure. 1631 */ 1632 static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 1633 unsigned long count, loff_t to, size_t *retlen) 1634 { 1635 unsigned long i; 1636 size_t totlen = 0, thislen; 1637 int ret = 0; 1638 1639 for (i = 0; i < count; i++) { 1640 if (!vecs[i].iov_len) 1641 continue; 1642 ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen, 1643 vecs[i].iov_base); 1644 totlen += thislen; 1645 if (ret || thislen != vecs[i].iov_len) 1646 break; 1647 to += vecs[i].iov_len; 1648 } 1649 *retlen = totlen; 1650 return ret; 1651 } 1652 1653 /* 1654 * mtd_writev - the vector-based MTD write method 1655 * @mtd: mtd device description object pointer 1656 * @vecs: the vectors to write 1657 * @count: count of vectors in @vecs 1658 * @to: the MTD device offset to write to 1659 * @retlen: on exit contains the count of bytes written to the MTD device. 1660 * 1661 * This function returns zero in case of success and a negative error code in 1662 * case of failure. 1663 */ 1664 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs, 1665 unsigned long count, loff_t to, size_t *retlen) 1666 { 1667 *retlen = 0; 1668 if (!(mtd->flags & MTD_WRITEABLE)) 1669 return -EROFS; 1670 if (!mtd->_writev) 1671 return default_mtd_writev(mtd, vecs, count, to, retlen); 1672 return mtd->_writev(mtd, vecs, count, to, retlen); 1673 } 1674 EXPORT_SYMBOL_GPL(mtd_writev); 1675 1676 /** 1677 * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size 1678 * @mtd: mtd device description object pointer 1679 * @size: a pointer to the ideal or maximum size of the allocation, points 1680 * to the actual allocation size on success. 1681 * 1682 * This routine attempts to allocate a contiguous kernel buffer up to 1683 * the specified size, backing off the size of the request exponentially 1684 * until the request succeeds or until the allocation size falls below 1685 * the system page size. This attempts to make sure it does not adversely 1686 * impact system performance, so when allocating more than one page, we 1687 * ask the memory allocator to avoid re-trying, swapping, writing back 1688 * or performing I/O. 1689 * 1690 * Note, this function also makes sure that the allocated buffer is aligned to 1691 * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value. 1692 * 1693 * This is called, for example by mtd_{read,write} and jffs2_scan_medium, 1694 * to handle smaller (i.e. degraded) buffer allocations under low- or 1695 * fragmented-memory situations where such reduced allocations, from a 1696 * requested ideal, are allowed. 1697 * 1698 * Returns a pointer to the allocated buffer on success; otherwise, NULL. 1699 */ 1700 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size) 1701 { 1702 gfp_t flags = __GFP_NOWARN | __GFP_WAIT | 1703 __GFP_NORETRY | __GFP_NO_KSWAPD; 1704 size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE); 1705 void *kbuf; 1706 1707 *size = min_t(size_t, *size, KMALLOC_MAX_SIZE); 1708 1709 while (*size > min_alloc) { 1710 kbuf = kmalloc(*size, flags); 1711 if (kbuf) 1712 return kbuf; 1713 1714 *size >>= 1; 1715 *size = ALIGN(*size, mtd->writesize); 1716 } 1717 1718 /* 1719 * For the last resort allocation allow 'kmalloc()' to do all sorts of 1720 * things (write-back, dropping caches, etc) by using GFP_KERNEL. 1721 */ 1722 return kmalloc(*size, GFP_KERNEL); 1723 } 1724 EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to); 1725 #endif 1726 1727 #ifdef CONFIG_PROC_FS 1728 1729 /*====================================================================*/ 1730 /* Support for /proc/mtd */ 1731 1732 static int mtd_proc_show(struct seq_file *m, void *v) 1733 { 1734 struct mtd_info *mtd; 1735 1736 seq_puts(m, "dev: size erasesize name\n"); 1737 mutex_lock(&mtd_table_mutex); 1738 mtd_for_each_device(mtd) { 1739 seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n", 1740 mtd->index, (unsigned long long)mtd->size, 1741 mtd->erasesize, mtd->name); 1742 } 1743 mutex_unlock(&mtd_table_mutex); 1744 return 0; 1745 } 1746 1747 static int mtd_proc_open(struct inode *inode, struct file *file) 1748 { 1749 return single_open(file, mtd_proc_show, NULL); 1750 } 1751 1752 static const struct file_operations mtd_proc_ops = { 1753 .open = mtd_proc_open, 1754 .read = seq_read, 1755 .llseek = seq_lseek, 1756 .release = single_release, 1757 }; 1758 #endif /* CONFIG_PROC_FS */ 1759 1760 /*====================================================================*/ 1761 /* Init code */ 1762 1763 #ifndef __UBOOT__ 1764 static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name) 1765 { 1766 int ret; 1767 1768 ret = bdi_init(bdi); 1769 if (!ret) 1770 ret = bdi_register(bdi, NULL, "%s", name); 1771 1772 if (ret) 1773 bdi_destroy(bdi); 1774 1775 return ret; 1776 } 1777 1778 static struct proc_dir_entry *proc_mtd; 1779 1780 static int __init init_mtd(void) 1781 { 1782 int ret; 1783 1784 ret = class_register(&mtd_class); 1785 if (ret) 1786 goto err_reg; 1787 1788 ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap"); 1789 if (ret) 1790 goto err_bdi1; 1791 1792 ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap"); 1793 if (ret) 1794 goto err_bdi2; 1795 1796 ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap"); 1797 if (ret) 1798 goto err_bdi3; 1799 1800 proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops); 1801 1802 ret = init_mtdchar(); 1803 if (ret) 1804 goto out_procfs; 1805 1806 return 0; 1807 1808 out_procfs: 1809 if (proc_mtd) 1810 remove_proc_entry("mtd", NULL); 1811 err_bdi3: 1812 bdi_destroy(&mtd_bdi_ro_mappable); 1813 err_bdi2: 1814 bdi_destroy(&mtd_bdi_unmappable); 1815 err_bdi1: 1816 class_unregister(&mtd_class); 1817 err_reg: 1818 pr_err("Error registering mtd class or bdi: %d\n", ret); 1819 return ret; 1820 } 1821 1822 static void __exit cleanup_mtd(void) 1823 { 1824 cleanup_mtdchar(); 1825 if (proc_mtd) 1826 remove_proc_entry("mtd", NULL); 1827 class_unregister(&mtd_class); 1828 bdi_destroy(&mtd_bdi_unmappable); 1829 bdi_destroy(&mtd_bdi_ro_mappable); 1830 bdi_destroy(&mtd_bdi_rw_mappable); 1831 } 1832 1833 module_init(init_mtd); 1834 module_exit(cleanup_mtd); 1835 #endif 1836 1837 MODULE_LICENSE("GPL"); 1838 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 1839 MODULE_DESCRIPTION("Core MTD registration and access routines"); 1840