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