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