linux/drivers/mtd/mtdcore.c
<<
>>
Prefs
   1/*
   2 * Core registration and callback routines for MTD
   3 * drivers and users.
   4 *
   5 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
   6 * Copyright © 2006      Red Hat UK Limited 
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21 *
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/kernel.h>
  26#include <linux/ptrace.h>
  27#include <linux/seq_file.h>
  28#include <linux/string.h>
  29#include <linux/timer.h>
  30#include <linux/major.h>
  31#include <linux/fs.h>
  32#include <linux/err.h>
  33#include <linux/ioctl.h>
  34#include <linux/init.h>
  35#include <linux/proc_fs.h>
  36#include <linux/idr.h>
  37#include <linux/backing-dev.h>
  38#include <linux/gfp.h>
  39
  40#include <linux/mtd/mtd.h>
  41#include <linux/mtd/partitions.h>
  42
  43#include "mtdcore.h"
  44/*
  45 * backing device capabilities for non-mappable devices (such as NAND flash)
  46 * - permits private mappings, copies are taken of the data
  47 */
  48static struct backing_dev_info mtd_bdi_unmappable = {
  49        .capabilities   = BDI_CAP_MAP_COPY,
  50};
  51
  52/*
  53 * backing device capabilities for R/O mappable devices (such as ROM)
  54 * - permits private mappings, copies are taken of the data
  55 * - permits non-writable shared mappings
  56 */
  57static struct backing_dev_info mtd_bdi_ro_mappable = {
  58        .capabilities   = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  59                           BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  60};
  61
  62/*
  63 * backing device capabilities for writable mappable devices (such as RAM)
  64 * - permits private mappings, copies are taken of the data
  65 * - permits non-writable shared mappings
  66 */
  67static struct backing_dev_info mtd_bdi_rw_mappable = {
  68        .capabilities   = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  69                           BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  70                           BDI_CAP_WRITE_MAP),
  71};
  72
  73static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  74static int mtd_cls_resume(struct device *dev);
  75
  76static struct class mtd_class = {
  77        .name = "mtd",
  78        .owner = THIS_MODULE,
  79        .suspend = mtd_cls_suspend,
  80        .resume = mtd_cls_resume,
  81};
  82
  83static DEFINE_IDR(mtd_idr);
  84
  85/* These are exported solely for the purpose of mtd_blkdevs.c. You
  86   should not use them for _anything_ else */
  87DEFINE_MUTEX(mtd_table_mutex);
  88EXPORT_SYMBOL_GPL(mtd_table_mutex);
  89
  90struct mtd_info *__mtd_next_device(int i)
  91{
  92        return idr_get_next(&mtd_idr, &i);
  93}
  94EXPORT_SYMBOL_GPL(__mtd_next_device);
  95
  96static LIST_HEAD(mtd_notifiers);
  97
  98
  99#if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
 100#define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
 101#else
 102#define MTD_DEVT(index) 0
 103#endif
 104
 105/* REVISIT once MTD uses the driver model better, whoever allocates
 106 * the mtd_info will probably want to use the release() hook...
 107 */
 108static void mtd_release(struct device *dev)
 109{
 110        struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
 111        dev_t index = MTD_DEVT(mtd->index);
 112
 113        /* remove /dev/mtdXro node if needed */
 114        if (index)
 115                device_destroy(&mtd_class, index + 1);
 116}
 117
 118static int mtd_cls_suspend(struct device *dev, pm_message_t state)
 119{
 120        struct mtd_info *mtd = dev_get_drvdata(dev);
 121
 122        return mtd ? mtd_suspend(mtd) : 0;
 123}
 124
 125static int mtd_cls_resume(struct device *dev)
 126{
 127        struct mtd_info *mtd = dev_get_drvdata(dev);
 128
 129        if (mtd)
 130                mtd_resume(mtd);
 131        return 0;
 132}
 133
 134static ssize_t mtd_type_show(struct device *dev,
 135                struct device_attribute *attr, char *buf)
 136{
 137        struct mtd_info *mtd = dev_get_drvdata(dev);
 138        char *type;
 139
 140        switch (mtd->type) {
 141        case MTD_ABSENT:
 142                type = "absent";
 143                break;
 144        case MTD_RAM:
 145                type = "ram";
 146                break;
 147        case MTD_ROM:
 148                type = "rom";
 149                break;
 150        case MTD_NORFLASH:
 151                type = "nor";
 152                break;
 153        case MTD_NANDFLASH:
 154                type = "nand";
 155                break;
 156        case MTD_DATAFLASH:
 157                type = "dataflash";
 158                break;
 159        case MTD_UBIVOLUME:
 160                type = "ubi";
 161                break;
 162        default:
 163                type = "unknown";
 164        }
 165
 166        return snprintf(buf, PAGE_SIZE, "%s\n", type);
 167}
 168static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
 169
 170static ssize_t mtd_flags_show(struct device *dev,
 171                struct device_attribute *attr, char *buf)
 172{
 173        struct mtd_info *mtd = dev_get_drvdata(dev);
 174
 175        return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
 176
 177}
 178static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
 179
 180static ssize_t mtd_size_show(struct device *dev,
 181                struct device_attribute *attr, char *buf)
 182{
 183        struct mtd_info *mtd = dev_get_drvdata(dev);
 184
 185        return snprintf(buf, PAGE_SIZE, "%llu\n",
 186                (unsigned long long)mtd->size);
 187
 188}
 189static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
 190
 191static ssize_t mtd_erasesize_show(struct device *dev,
 192                struct device_attribute *attr, char *buf)
 193{
 194        struct mtd_info *mtd = dev_get_drvdata(dev);
 195
 196        return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
 197
 198}
 199static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
 200
 201static ssize_t mtd_writesize_show(struct device *dev,
 202                struct device_attribute *attr, char *buf)
 203{
 204        struct mtd_info *mtd = dev_get_drvdata(dev);
 205
 206        return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
 207
 208}
 209static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
 210
 211static ssize_t mtd_subpagesize_show(struct device *dev,
 212                struct device_attribute *attr, char *buf)
 213{
 214        struct mtd_info *mtd = dev_get_drvdata(dev);
 215        unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
 216
 217        return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
 218
 219}
 220static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
 221
 222static ssize_t mtd_oobsize_show(struct device *dev,
 223                struct device_attribute *attr, char *buf)
 224{
 225        struct mtd_info *mtd = dev_get_drvdata(dev);
 226
 227        return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
 228
 229}
 230static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
 231
 232static ssize_t mtd_numeraseregions_show(struct device *dev,
 233                struct device_attribute *attr, char *buf)
 234{
 235        struct mtd_info *mtd = dev_get_drvdata(dev);
 236
 237        return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
 238
 239}
 240static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
 241        NULL);
 242
 243static ssize_t mtd_name_show(struct device *dev,
 244                struct device_attribute *attr, char *buf)
 245{
 246        struct mtd_info *mtd = dev_get_drvdata(dev);
 247
 248        return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
 249
 250}
 251static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
 252
 253static ssize_t mtd_ecc_strength_show(struct device *dev,
 254                                     struct device_attribute *attr, char *buf)
 255{
 256        struct mtd_info *mtd = dev_get_drvdata(dev);
 257
 258        return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
 259}
 260static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
 261
 262static ssize_t mtd_bitflip_threshold_show(struct device *dev,
 263                                          struct device_attribute *attr,
 264                                          char *buf)
 265{
 266        struct mtd_info *mtd = dev_get_drvdata(dev);
 267
 268        return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
 269}
 270
 271static ssize_t mtd_bitflip_threshold_store(struct device *dev,
 272                                           struct device_attribute *attr,
 273                                           const char *buf, size_t count)
 274{
 275        struct mtd_info *mtd = dev_get_drvdata(dev);
 276        unsigned int bitflip_threshold;
 277        int retval;
 278
 279        retval = kstrtouint(buf, 0, &bitflip_threshold);
 280        if (retval)
 281                return retval;
 282
 283        mtd->bitflip_threshold = bitflip_threshold;
 284        return count;
 285}
 286static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
 287                   mtd_bitflip_threshold_show,
 288                   mtd_bitflip_threshold_store);
 289
 290static struct attribute *mtd_attrs[] = {
 291        &dev_attr_type.attr,
 292        &dev_attr_flags.attr,
 293        &dev_attr_size.attr,
 294        &dev_attr_erasesize.attr,
 295        &dev_attr_writesize.attr,
 296        &dev_attr_subpagesize.attr,
 297        &dev_attr_oobsize.attr,
 298        &dev_attr_numeraseregions.attr,
 299        &dev_attr_name.attr,
 300        &dev_attr_ecc_strength.attr,
 301        &dev_attr_bitflip_threshold.attr,
 302        NULL,
 303};
 304
 305static struct attribute_group mtd_group = {
 306        .attrs          = mtd_attrs,
 307};
 308
 309static const struct attribute_group *mtd_groups[] = {
 310        &mtd_group,
 311        NULL,
 312};
 313
 314static struct device_type mtd_devtype = {
 315        .name           = "mtd",
 316        .groups         = mtd_groups,
 317        .release        = mtd_release,
 318};
 319
 320/**
 321 *      add_mtd_device - register an MTD device
 322 *      @mtd: pointer to new MTD device info structure
 323 *
 324 *      Add a device to the list of MTD devices present in the system, and
 325 *      notify each currently active MTD 'user' of its arrival. Returns
 326 *      zero on success or 1 on failure, which currently will only happen
 327 *      if there is insufficient memory or a sysfs error.
 328 */
 329
 330int add_mtd_device(struct mtd_info *mtd)
 331{
 332        struct mtd_notifier *not;
 333        int i, error;
 334
 335        if (!mtd->backing_dev_info) {
 336                switch (mtd->type) {
 337                case MTD_RAM:
 338                        mtd->backing_dev_info = &mtd_bdi_rw_mappable;
 339                        break;
 340                case MTD_ROM:
 341                        mtd->backing_dev_info = &mtd_bdi_ro_mappable;
 342                        break;
 343                default:
 344                        mtd->backing_dev_info = &mtd_bdi_unmappable;
 345                        break;
 346                }
 347        }
 348
 349        BUG_ON(mtd->writesize == 0);
 350        mutex_lock(&mtd_table_mutex);
 351
 352        do {
 353                if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
 354                        goto fail_locked;
 355                error = idr_get_new(&mtd_idr, mtd, &i);
 356        } while (error == -EAGAIN);
 357
 358        if (error)
 359                goto fail_locked;
 360
 361        mtd->index = i;
 362        mtd->usecount = 0;
 363
 364        /* default value if not set by driver */
 365        if (mtd->bitflip_threshold == 0)
 366                mtd->bitflip_threshold = mtd->ecc_strength;
 367
 368        if (is_power_of_2(mtd->erasesize))
 369                mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
 370        else
 371                mtd->erasesize_shift = 0;
 372
 373        if (is_power_of_2(mtd->writesize))
 374                mtd->writesize_shift = ffs(mtd->writesize) - 1;
 375        else
 376                mtd->writesize_shift = 0;
 377
 378        mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
 379        mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
 380
 381        /* Some chips always power up locked. Unlock them now */
 382        if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
 383                error = mtd_unlock(mtd, 0, mtd->size);
 384                if (error && error != -EOPNOTSUPP)
 385                        printk(KERN_WARNING
 386                               "%s: unlock failed, writes may not work\n",
 387                               mtd->name);
 388        }
 389
 390        /* Caller should have set dev.parent to match the
 391         * physical device.
 392         */
 393        mtd->dev.type = &mtd_devtype;
 394        mtd->dev.class = &mtd_class;
 395        mtd->dev.devt = MTD_DEVT(i);
 396        dev_set_name(&mtd->dev, "mtd%d", i);
 397        dev_set_drvdata(&mtd->dev, mtd);
 398        if (device_register(&mtd->dev) != 0)
 399                goto fail_added;
 400
 401        if (MTD_DEVT(i))
 402                device_create(&mtd_class, mtd->dev.parent,
 403                              MTD_DEVT(i) + 1,
 404                              NULL, "mtd%dro", i);
 405
 406        pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
 407        /* No need to get a refcount on the module containing
 408           the notifier, since we hold the mtd_table_mutex */
 409        list_for_each_entry(not, &mtd_notifiers, list)
 410                not->add(mtd);
 411
 412        mutex_unlock(&mtd_table_mutex);
 413        /* We _know_ we aren't being removed, because
 414           our caller is still holding us here. So none
 415           of this try_ nonsense, and no bitching about i1aan>
mtd_devtype =4"L31641 326                retu = 0;
 319
 320fail_added        ing reme=i" class="sref">ing remock(&mtd_idr, i);
 322fail_locked        mutex_unlock(&mtd_table_mutex);
        retu - 1;
}
 326}
 327/**
 328     del_md_mtd_deviceun - register an MTD device
 329 *      @mtd: pointer ew MTD device info structure
 *
 *   Rg remAdd a devifrom to the list of MTD devices present in the syst> *
 *   e, and notify each currently active MTD 'user' of ideparructice.
 *   Rg Retu    zero on success or 1 on failure, which currently wily happen
 *      if    que sydd a devidovicay ny haanter beces present in the lice.

}
 337<   int (struct mtd_info *mtd)
 338<)) {
 339        int  340        struct mtd_notifier *not;

        mutex_lock(&mtd_table_mutex);

 344        if (ifin=mtd" class="sred">ifin=ock(&mtd_idr, mtd->index) > *mtd)) {
 345             nt  346                goto  347        }
 348
 349        /* No need to get a refcount on the module containing
        list_for_each_entry(not, &mtd_notifiers, list)
                not->mtd);

        if (mtd->usecount)) {
 355                printk(&quRg retchiew MTD devi#ce %(to )tlyth f"> refcouce ork\n",
                    >, mtd->index, mtd->name, mtd->usecount);
             nt  358     }    e>)) {
 359                deviun - regisock(&mtd->dev);
 360
                ing reme=i" class="sref">ing remock(&mtd_idr, mtd->index);

                 364             nt  365        }
}
 367        mutex_unlock(&mtd_table_mutex);
 369        retunt  370<   }

/**
md_mtd_dev_parsvice_regis - parsv partitregi e, a - register an MTD device.

   @mtld tew MTD devito  - regisoc.
@devts: to the list of Mpartitreg probesice try, seture
 *      ser&#parsvimd_mpartitregi()serfor morvice irmatregure
@parsvr_drvd: of Mpartitreg parsvr-specific drvdate
@parts: fallf">bMpartitreg ce irmatregito  - regis,    parstchion fs;ate
 *      ll onvalid    ce &#nrmparts a>-& ce �ate
@nrmparts: to tnumbistof partitregi ig parts,       zeto t on tfullate
 *         ew MTD devics  - regisedlue ifMpartitreg ce iics fsec and
 *
Tf thfunctreg agg - awritof Mpartitregs parstchi(done by> *
ser&#parsvimd_mpartitregi()ser&#) e, aew MTD devie, apartitregs  - registch. It i1aan>
baphysi onfollows on the lis="ceg patgisn fsec  ig manyaew MTrivers: i1aan>

* It fir litriesice probeapartitregs or an MTD dev>   @ ustchiparsvrurns
 *specifie  ig @devts (ue @devts  thce &#f">N,eto t on t/* defauhe list parsvrurns
 * thusmoveset ser&#parsvimd_mpartitregi()serfor morvice irmatreg). If So n we rns
 391 *fsec  tf thfunctregsitriesice fallf">bMce ce irmatregispecifie  igrns
 392 *@parts/@nrmpartsice.
* If anyapartitregtchice iiwas fsec , tf thfunctreg  - regiss on tfsec and
 *partitregiice.
* If Soapartitregs w thefsec  tf thfunctreg ju li - regiss on tan MTD device
 *   @ e, aexitsice.

Rg Retu    zeig    cast on succee, aa n- awactide=er ="+ceig    cast on failice.

int mtd_dev_parsvice_regisock(struct mtd_info *mtd<,ule  lishar *a> *                           a>(struct mpart_parsvr_drvdnfo * 402                           le  lia>(struct mpartitreguro * 403                           /a>int  404<)) {
      a>int  406     a>(struct mpartitreguro *
        mtd,  409        if ( 410                sof(a> *p;                                      GFP_KERNEL);
                if (!                                        else
                        mtd_devtype =5"L31651 356      a>;

        if (                mtd,                      }    e>   if (                mtd);
                if (                                }
 326}
        retunt  328<   }
 329(mtd_dev_parsvice_regisock);

/**
f">mtd_dev_un - registceun - register exregtchiew MTD device.

@maegismtld tew MTD devito un - regis. >Tf thly wiun - registboth on thaegisoc.


 337<   int mtd_dev_un - registex" class="sref">mtd_dev_un - regisock(struct mtd_info * 338<)) {
 339        int 
        (        if (                retunt  344;
 345        if (!(&dev
 346                retu = 0;

        retu = ( 349<   }
mtd_dev_un - regisock;

/**
 *    - regis_md_m39;utce - registeMTD 'user' of MTD deviice.
 *   @newmtd: pointer td_notifice info structure

 *   Rg regiss aapaistof callf">bthfunctreg er becur cad upeg additregure
 *   oring real39; of MTD devii Cbecas on tser&#addsercallf">bMce beci"codiately> *
     invokad for fy eaew MTD devich currentes present in the sysice.

 360, mtd_notifier *     a>(struct mtd_info *mtd;

 364     > *mutex_lock(&mtd_table_mutex);

        lide=add" class="sref">lide=ock(&list, &mtd_notifiers);

        
     > *mst_for_eatd_devdev" class="sred">mst_for_eatd_devock(mtd
                add(mtd);

        mutex_unlock(&mtd_table_mutex);
}
/**
>>>>>un - regis_md_m39;utceun - registeMTD 'user' of MTD deviice.
 *   @oldmtd: pointer td_notifice info structure

 *   Rg rems aacallf">bMfunctreg paistfrom to the list TD 'usserce beure
 *   td_notid upeg additreg oring real39; of MTD devii Cbecas on ure
 *   ser&#ng remsercallf">bMce beci"codiately invokad for fy eaew MTD devure
>>>>>ch currentes present in the sysice.

mtd_notifier *     a>(struct mtd_info *mtd;

     > *mutex_lock(&mtd_table_mutex);

        
     > *mst_for_eatd_devdev" class="sred">mst_for_eatd_devock(mtd
                mtd);
}
        lideladd" class="sref">lidelock(&list);
        mutex_unlock(&mtd_table_mutex);
        retu = 0;

/**
>>>>>get_md_mtd_devtceobcont aavalidasyddhandle for er an MTD device

 *   @num:  poinnal3td_devtnumbistof if    quisedlan MTD device

 408 *   Givet aanumbiste, af">N addrucc,    retuto tnumrenh ch_enent in tTD device
 *   td_ta,    any.  Givet ag addruccee, anum/a> -1vesearch in tTD dev td_taice
 *   for etTD dev lyth that addruccee, a   retui; ofren&#r is sties pres. Givetice
 *   both,    retuto tnumrenh f="dri ll oni; of iaddruccematchvii Rg Retice
 *   de=er ="+ceif Solice.

(struct mtd_info *mtd_info *mtd<,u/a>int mtd_devtype =6"L31661 406     a>(struct mtd_info *Nmtd" class="sref">Nmtd<,ua> *        int 
     > *mutex_lock(&mtd_table_mutex);

        if (                mst_for_eatd_devdev" class="sred">mst_for_eatd_devock(             /a>        if (mtd)) {
                                mtd;
                             break/a>;
                        }
                }
     }    e>   if (                ifin=mtd" class="sred">ifin=ock(&mtd_idr,                 if (mtd && mtd) > *                     > *Nmtd" class="sref">Nmtd;
        }

        if (!             nt                 goto         }

     > *        ia>(                        mutex_unlock(&mtd_table_mutex);
        retunt  345<   }
 346

 349<   int mtd_info *mtd;
        int 
        if (!mtd->                retu!= -
        if (mtd->             nt mtd->mtd);

 359                ia>(             /a>        mtd->             /a>        retunt                 }
        }
 364     > *mtd-> 365        retu = 0;
 367
/**
 *   get_md_mtd_dev_nmtceobcont aavalidasyddhandle for er an MTD dev by> *
 *   td_devtn" n> *
 *   @n" n: an MTD dev n" nMce opetice

 *   Tf thfunctreg  - Retu an MTD dev descriptregiso structeig    castice
>>>>>on succee, aanide=er ="+ceig    cast on failice.

(struct mtd_info * d;
        int      a>(struct mtd_info *mtd< > > *Nmtd" class="sref">Nmtd<,ua> *
        mutex_lock(&mtd_table_mutex);

     > *mst_for_eatd_devdev" class="sred">mst_for_eatd_devock(                if (! d,  d)) {
                     > *mtd< > > *                     break/a>;
     /a>        }
        }

        if (!mtd;
                goto 
     > *mtd);
        ia>(                goto 
        mutex_unlock(&mtd_table_mutex);
        retua>(mtd);

        mutex_unlock(&mtd_table_mutex);
 403        retua>(}
, mtd_info *mtd;
     > *mutex_lock(&mtd_table_mutex);
     > *mtd);
        mutex_unlock(&mtd_table_mutex);


mtd_devtype =7"L31671 406, mtd_info *mtd;
     -!= -mtd->     > *mtd->
        if (mtd->                mtd" class="sref">mtd->mtd);

     > *mtd->

Er  cais ag asynchronous operatreg.  DD dev d="driv are supposedoce
to3call ina>(/a>-&callf">b() whenedri to toperatreg s="pletes, evetice
i; of s="pletes lyth a on failice.
Cr caiv are supposed>to3pass aacallf">bMfunctreg e, awaof for itce.


mer  cnfo" class="sref">mer  cock(struct mtd_info *mtd<, a>(struct (nfo" class="sreina>(mtd;
        if (mtd" class="sref">mtd->(nfo" class="sreina>(mtd->mtd" class="sref">mtd->(nfo" class="sreina>(mtd->     /a>        retu!= - 339        if/a>(mtd->                retu!= -                if (!              (!                mer  c_callf">bnfo" class="sref">mer  c_callf">bock(                retu = 0;
 346        }
        retua>(mtd->mtd<, a>( 349mer  cnfo" class="sref">mer  cock);


Nice.

md: ponfo" class="sref">md: poock(struct mtd_info *mtd<, gt;           voida*a> *     a> *     a> *Nmtd" class="sref">Nmtd;
        i> *             a> *        if (!mtd->                retu!= -        igt;mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(md: ponfo" class="sref">md: poock
N "x */
mund: ponfo" class="sref">mund: poock(struct mtd_info *mtd<, gt;        if (!mtd->                retu!= -        if (mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mund: ponfo" class="sref">mund: poock

Allow NOMMU mmap()ier directly map in tTD dev (if Solaf">N)ice
-    retuto taddrucceer which in toffsetimapsice
-    retu-ENOSYSeer indicat    fusalier dr to tmappingice

mget_unmapped_areaock(struct mtd_info *mtd<, unsigned longuct              /a>                 unsigned longuct         if (!mtd->                retu!= -        ict mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        retua>(mtd->mtd<, a>(mget_unmapped_areanfo" class="sref">mget_unmapped_areaock<=a>);
);
mrea=mtd" class="sref">mrea=ock(struct mtd_info *mtd<, gt;          > * 403        int );
     a> *        ia>(mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!     /a>        retu = 0;
);
     >a>

     nt  = mtd->mtd<, a>(mtd_devtype =8"L31681 386        if ();
                retunt );
        i = mtd->                retu =a>     >a>
        retua>(-&a> = mtd->mrea=mtd" class="sref">mrea=ock<== 0;

mwrigcnfo" class="sref">mwrigcock(struct mtd_info *mtd<, gt;           le  li> *     a> *        i = mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!mtd->mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mwrigcnfo" class="sref">mwrigcock<== 0;


In blackbox flight   mtdder like scenarios we wantier make sn succful wrigesice
in    errupf s=  ext. >to3becur cad when itsice
knowtuto tkernelais about>to3pasic e, awe need>the wrignMce sn sued. Sincn> *

Solasleep)ice.

m>m>(struct mtd_info *mtd<, gt;                 le  li> *     a> *        ifa>(mtd->                retu!= -        if (mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if/a>(mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mdm>
mrea=_oobmtd" class="sref">mrea=_oobock(struct mtd_info *mtd<, gt;        int );
     > *        if (!mtd->                retu!= -     /a>
In cases lhere opi/a>-&datbuf !=af">N, td//a>-&mrea=_oob() has semanticsice
similarier md//a>-&mrea=(),    rettchia non-negat"dr    eg tmte
represengtchimax bitflips.>In oif t cases, td//a>-&mrea=_oob() maymte
re retu-EUCLEAN.>In all cases, perform>similarilogicier md/mrea=()ice.
     > * = mtd->mtd<, a>(        i> *);
                retunt );
        if (mtd->                retu =a>     >a>
        retua>(-&a> = mtd->mrea=_oobmtd" class="sref">mrea=_oobock<=/a>;
;

to3a succethe protectreg regrst t area, preseng>in some fn chice
To tus t dataais one time programmd_ta but>the factory dataais rea=oce
onlyice.

mget_fact_protmtd_info" class="sref">mget_fact_protmtd_iock(struct mtd_info *mtd<, a>(struct      /a>                gt;        if (!mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mget_fact_protmtd_info" class="sref">mget_fact_protmtd_iock
mrea=_fact_protmregmtd" class="sref">mrea=_fact_protmregock(struct mtd_info *mtd<, gt;                        gt;     a> *        if (!mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mrea=_fact_protmregmtd" class="sref">mrea=_fact_protmregock<== 0;
);
mget_user_protmtd_info" class="sref">mget_user_protmtd_iock(struct mtd_info *mtd<, a>(struct                         gt;        ifa>(mtd->                retu!= -        if (!mtd_devtype =9"L31691 346                retu = 0;
        retua>(mtd->mtd<, a>(mget_user_protmtd_info" class="sref">mget_user_protmtd_iock
mrea=_user_protmregmtd" class="sref">mrea=_user_protmregock(struct mtd_info *mtd<, gt;                        gt;     a> *        if (!mtd->                retu!= -        if (!     /a>        retu = 0;
        retua>(mtd->mtd<, a>(mrea=_user_protmregmtd" class="sref">mrea=_user_protmregock<== 0;
mwrigc_user_protmregmtd" class="sref">mwrigc_user_protmregock(struct mtd_info *mtd<, gt;                         gt;     a> *        if (!mtd->     /a>        retu!= -        ifa>(                retu = 0;
        retua>(mtd->mtd<, a>(mwrigc_user_protmregmtd" class="sref">mwrigc_user_protmregock<== 0;
m_unl_user_protmregmtd" class="sref">m_unl_user_protmregock(struct mtd_info *mtd<, gt;        if (!mtd->     /a>        retu!= -        ifa>(                retu = 0;
        retua>(mtd->mtd<, a>(m_unl_user_protmregmtd" class="sref">m_unl_user_protmregock<== 0;

m_unlmtd" class="sref">m_unlock(struct mtd_info *mtd<, gt;        if (!mtd->                retu!= -        igt;mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(m_unlmtd" class="sref">m_unlock<== 0;

mun_unlmtd" class="sref">mun_unlock(struct mtd_info *mtd<, gt;        if (!mtd->                retu!= -        igt;mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!                retu = 0;
        retua>(mtd->mtd<, a>(mun_unlmtd" class="sref">mun_unlock<== 0;
);
mism_unle=mtd" class="sref">mism_unle=ock(struct mtd_info *mtd<, gt;        if (!mtd->                retu!= -        i> *mtd" class="sref">mtd->mtd" class="sref">mtd->                retu!= -        if (!                retu =>);
        retua>(mtd->mtd<, a>(mism_unle=mtd" class="sref">mism_unle=ock<== 0;
;
mb_unl_isba=mtd" class="sref">mb_unl_isba=ock(struct mtd_info *mtd<, gt;        if (!mtd->                retu = 0;
        if (mtd" class="sref">mtd->                retu!= -        retua>(mtd->mtd<, a>(10#L410mb_unl_isba=mtd" class="sref">mb_unl_isba=ock<== 0/pre> }
10# 411;
10# 412<   int mb_unl_markba=mtd" class="sref">mb_unl_markba=ock(struct mtd_info *mtd<, gt;10# 323<)) {
10# 344        if (!mtd->10# 345                retu!= -10# 386        if (mtd" class="sref">mtd->10# 387                retu!= -10#8344        if/a>(mtd->10# 359                retu!= -10"L344        retua>(mtd->mtd<, a>(10"1359<   }
10"2410mb_unl_markba=mtd" class="sref">mb_unl_markba=ock<== 0;
10"3411;
10" 324
10" 325default_m">mwrigcv ->the default wrigcv methodice
mtd_devtype =10"sr>10" 326
10"7326the vectors>to3wrigcoce
10" 408in @vecsoce
10" 329the MTD TD dev offset>to3wrigc tomte
10"L329to3the MTD TD device.
10" 321
10" 32210" 323
10" 324
10" 325mwrigcvmtd" class="sredefault_m">mwrigcvock(struct mtd_info *mtd<, const a>(struct 10" 346                           unsigned longuct 10" 327<)) {
10"8344     unsigned longuct 10" 359     gt;10"L344        int 10" 411;
10" 412     for /a>(10" 403                if (!10" 344                     continuea>);
10" 345             nt mwrigcock *mtd<, a>(10" 346                             nt 10" 387             gt;10" 378     /a>        if (10" 359                     breaka>);
10"L387             gt;10" 371        }
10" 412     a> *10" 403        retua>(10"4359<   }
10"5411;
10" 326
10"7326mwrigcv ->the vector-based MTD wrigc methodice
10" 408
10" 329the vectors>to3wrigcoce
10"L408in @vecsoce
10" 321the MTD TD dev offset>to3wrigc tomte
10" 32210" 323
10" 324in case of su succeandia negat"dr error ="+c>inice
10" 325case of failurvice.
10" 326
10" 327<   int mwrigcvmtd" class="srem">mwrigcvock(struct mtd_info *mtd<, const a>(struct 10" 378     /a>    unsigned longuct 10" 339<)) {
10"L412     a> *10" 371        if/a>(mtd->10" 402                retu!= -10"3344        ifa>(mtd->10" 344                retunt mwrigcvmtd" class="sredefault_m">mwrigcvock *mtd<, a>(10" 345        retua>(mtd->mtd<, a>(10"6359<   }
10"7326(mwrigcvmtd" class="srem">mwrigcvock<=a>);
10"8411;
10" 329
10"L326mkmalloc_up_to3- allocagc a contiguous buffer up>to3the specified sizcmt.
10" 321
10" 32210" 323
10" 324
10" 325This rout="l attempts>to3allocagc a contiguous kernel buffer up>toce.
10" 326
10"7326
10" 408This attempts>to3make sure it does not addrivelyce.
10" 329
10"L329to3avoid re-tryrch, swapprch, wrigrchibanlmt.
10" 321performrchiI/Oice.
10" 32210" 323
10" 324the "td//a>-&wrigcsizc" valuvice.
10" 325
10" 326m{rea=,wrigc}eandijffs2_scan_medium,ce.
10"7326degraded) buffer allocagregs under low- otce.
10" 408
 d);

e.e.ame10"L3>10" 325
e.ename10"L3>10" 321
e.ename10"L3>10" 322
e.ename1llocaf">mtd_info *m+cof">mwrigcvmtd" ccomment" m">mRT_SY"sref">mtd<, const a>(struct mtd_info *e.e4ame10"L3>10" 339<)) {
e.ename10"L3>10"esizc_lnfo gfphref="+cosizc_lisgfphrss="srefoff_lnfogt__GFP_WAITf="+co_wrigcvmtd_GFP_WAITclass="L3>10" 339<)) {
e.ename10"L3>10" 346          lenicdgt__GFP_NORETRYf="+co_wrigcvmtd_GFP_NORETRYclass=e   lenicdgt__GFP_NO_KSWAPDf="+co_wrigcvmtd_GFP_NO_KSWAPDass="sredROFSmtd);
e.e7ame10"L3>10"esizc_lnfo ;;mtd->quot;td// href="+code=mtquot;td//ss="s=="sretomtd<, gtPAGE_SIZoMTD_WRITEABEABLPAGE_SIZosrem">mwrigcvock<=a>);
e.ename10"L3>10"llocaf">mtd_info k;mwrigcvock<=a>);
e.e name9"L37997 369<>);
10"L412     a> ;gtmD _ref="+cosizc_lismD _rRT_SYMBOL_GPRock;mwrigcvock<=a>);
10# 411;
10"whimen(L412     a> ;-&    D _mment href="+code=mtdD _mmentass=s="sreimtd<++) )) {
10" 403">mtd_info k;gtcomment href="+cobufnfkmmmentass=(L412     a> ;mtd<, a>(10" 344     /a>        if k;10# 3450"L3>10" 345        retua>k;mwrigcvock<=a>);
);
10# 387L412     a> ;mwrigcvock<=a>);
10" 378L412     a> ;gtALIGNf="+co_wrigcvmtALIGNass=(L412     a> ;mtd<, a>(mtd->quot;td// href="+code=mtquot;td//ss="">mwrigcvock<=a>);
10#0"L3>10"6359<   }
10"6359<   }
10"0"L3>10" 326
10" 322L Femtd po" 3t" 3sort or until thure w he MTkmmment()he MTzc: d"l at sorts ofass="commen/"ice
10" 323L things (quot;-rch,, droe-try caches, etc), foustry GFP_KERNELc" valuvice.
10" 324Lass="comment"x */
10" 345        retua>comment href="+cobufnfkmmmentass=(L412     a> ;mtd<, a>GFP_KERNELhref="+cosizcnfGFP_KERNELss="">mwrigcvock<=a>);
mtd_d1vtype11name10"L3>10"6359<   }
10"7326(m+cof">mwrigcvmtd" ccomment" m">mRT_S">mwrigcvock<=a>);
10"8411;
mtd<, a>CONFIG_PROC_ href="+codROFSmtCONFIG_PROC_ hame10"L3>10"8411;
10"6359<   }
10" 321
10" 322Lass="comment"x */
10"3411;
10>mtd<, const a>(structproc_dir_sparyref="+codROFSmtproc_dir_sparyss="sresizc_lnfo proc_(10"5411;
10" 325mwrigcvmtd" cproc_showRT_SY>mtd<, const a>(structseq_fimehref="+cosizcnfoeq_fime="sref">mtd_info *+cof">mwrigcvmtdlasss=llocaf">mtd_info "+cof">mwrigcvmtss="sremwrigcvock<=>);
10" 327<)) {
10">mtd<, const a>(struct mtd_info *mwrigcvock<=a>);
);
10"const a>(structseq_puthref="+coflagierseq_puthRT_SYMBOL_GPRock(+cof">mwrigcvmtdlasss=0"L3>10" 322<>mttry">a>-&wrdev:>10"> all  erase> all 0" c\na>-&wrss="com">mwrigcvock<=a>);
10"const a>(structmutex href="+cof">m_unlmtdutex hrefRT_SYiov_lenicd<, &p;&aa>o" ctable_dutex="+code=mtd_info" ctable_dutexRT_S">mwrigcvock<=a>);
10"enicd<, &p;&aa>o" cs i_each_en "tt="+code=mtd_info" cs i_each_en "ttRT_SYMBOL_GPRock(10" 403const a>(structseq_pttrta href="+cobufnfseq_pttrtaRT_SYMBOL_GPRock(+cof">mwrigcvmtdlasss=0"L3>10" 322<>mttry">a>-&wref=he 7;d: he 7;8.8llx he 7;8.8x \a>-&wrhe 7;s\a>-&wr\na>-&wrss="com="srevecsocd<, 0;
10" 344        10"enicd<, &p;&aa>o"  href="+code=mtd" class="sref">mtd->index="+code=mtd_infindexlasss=(>     /a>    u    )enicd<, &p;&aa>o"  href="+code=mtd" class="sref">mtd->10" 345        10"enicd<, &p;&aa>o"  href="+code=mtd" class="sref">mtd->erase> al href="+code=mterase> allasss="sref">mtd<, a>(mtd->5" c href="+code=mt5" cRT_S">mwrigcvock<=a>);
10"0"L3>10"6359<   }
10"esizc_lnfo dutex href="+cof">mun_unlmtdutex href="RT_SYiov_lenicd<, &p;&aa>o" ctable_dutex="+code=mtd_info" ctable_dutexRT_S">mwrigcvock<=a>);
0a>);
10"6359<   }
10" 325mtd<, const a>(structinruc="+code=mtd_infinruc="sref">mtd_info inruc="+code=mtd_infinruc="sr,">mtd<, const a>(structfimehref="+cosizcnffime="sref">mtd_info fimehref="+cosizcnffime="srremwrigcvock<=>);
10" 403        retua>stryle_ophref="+co   lennfstryle_ophrRT_SYMBOL_GPRockfimehref="+cosizcnffime="srs="sref">mtd<, a>(mwrigcvmtd" cproc_showRT_Ss="sref">mtd<, a>NULL+cof">mwrigcvmtNULLRT_S">mwrigcvock<=a>);
10"4359<   }
10"5411;
10"sref">mtd<, const a>(structfime_ophrreducehref="+cosizcnffime_ophrreducess="sresizc_lnfo10"ss="sreimtd<].ctophref="+co   lennfophrRT_S        10"="sre   nfo10"ss="sreimtd<].ctmpleef="+co   lennfmpleRT_S        10"="sre   nfo10"ss="sreimtd<].ctllsee"+cof">mun_unlmtllsee"RT_S        1="sre   nfomun_unlmtseq_lsee"RT_Ss"srevecsocd<, 0;
10"ss="sreimtd<].ctmpleref="+coiov_basenfmplerefame10"L3>10"="sre   nfomwrigcvock<=a>);
10" 326Lass="comment"x */
10"3411;
10" 324
10" 325Lass="comment"x */
);
10" 325mtd<, const a>(structcified _en  href="+code=mtd_infcified _en  href="sref">mtd_info bdi="+code=mtd_infcdiRT_Ss="sref"charef">mtd_info 5" c href="+code=mt5" cRT_S"mwrigcvock<=a>);
10"L344        int mwrigcvock<=a>);
10"6359<   }
10"const a>(struct mtd_info bdi="+code=mtd_infcdiRT_S">mwrigcvock<=a>);
        if ( );
10" 403const a>(struct mtd_info bdi="+code=mtd_infcdiRT_Ss="sref">mtd<, a>NULL+cof">mwrigcvmtNULLRT_Ss="sref">mtd<, a>5" c href="+code=mt5" cRT_S">mwrigcvock<=a>);
);
10"     /a>        if ();
10" 346sre   nfomtoyref="+codROFSmtbdi_de>mtoyRT_SY">mtd_info bdi="+code=mtd_infcdiRT_S">mwrigcvock<=a>);
);
        retua> mwrigcvock<=a>);
10"6359<   }
10" 325);
10"L344        int mwrigcvock<=a>);
);
10"esizc_lnfo  o" cref="="+coiov_basenfT" c+coioRT_S">mwrigcvock<=a>);
10# 386        if ();
10# 387goto"sre   nfomwrigcvock<=a>);
10"8411;
10" 359     gt o" cbdi_unmappable="+coiov_basenfT" cbdi_unmappablelasss=0"L3>10" 322<>mttry">a>-&wref=-unmapa>-&wrss="com">mwrigcvock<=a>);
10# 386        if ();
10# 387goto"sre   nfomwrigcvock<=a>);
);
10" 359     gt o" cbdi_ro_mappable="+coiov_basenfT" cbdi_ro_mappablelasss=0"L3>10" 322<>mttry">a>-&wref=-romapa>-&wrss="com">mwrigcvock<=a>);
10# 344        if ();
10" 345goto"sre   nfomwrigcvock<=a>);
);
10"esizc_lnfo  o" cbdi_rw_mappable="+coiov_basenfT" cbdi_rw_mappablelasss=0"L3>10" 322<>mttry">a>-&wref=-rwmapa>-&wrss="com">mwrigcvock<=a>);
10#8344        if ();
10" 359goto"sre   nfomwrigcvock<=a>);
e19name1"L3>10"6359<   }
e1ename1#ifdef="sref">mtd<, a>CONFIG_PROC_ href="+codROFSmtCONFIG_PROC_ hame10"L3>10"8411;
e19name10"L3>10"enicd<, &p;&aa>proc_(10" 322<>mttry">a>-&wref=h>-&wrss="com=eti lennfoNULL+cof">mwrigcvmtNULLRT_Ss=iov_lenicd<, &p;&aa>o" cproc_opehref="+cosizcnfT" cproc_opelass">mwrigcvock<=a>);
e1ename1#endif"0"L3>10" 326Lass="comment"x */
e19name10#L3>10#83440a>);
e19name10"L3>10"5411;
e1ename1sre   nfo10"5411;
e1e7ame10"L3>10"esizc_lnfo bdi_de>mtoyref="+codROFSmtbdi_de>mtoyRT_SYiov_lenicd<, &p;&aa>o" cbdi_ro_mappable="+coiov_basenfT" cbdi_ro_mappablelass">mwrigcvock<=a>);
e1ename1sre   nfo10"5411;
e19name10"L3>10" 359     gtbdi_de>mtoyref="+codROFSmtbdi_de>mtoyRT_SYiov_lenicd<, &p;&aa>o" cbdi_unmappable="+coiov_basenfT" cbdi_unmappablelass">mwrigcvock<=a>);
10"5411;
10"const a>(structref="_unregister="+coiov_basenfref="_unregisterRT_SYiov_lenicd<, &p;&aa>o" cref="="+coiov_basenfT" c+coioRT_S">mwrigcvock<=a>);
10"5411;
10" 359     gtpr_err"+code=mwrigcnfpr_errRT_SY""L3>10" 322<>mttry">a>-&wrEra neregister whentd10" 32ass=bdi: he 7;d\na>-&wrss="com="esizc_lnfo  mwrigcvock<=a>);
10#8344        int mwrigcvock<=a>);
);
10lloca5);
mtd<, a>CONFIG_PROC_ href="+codROFSmtCONFIG_PROC_ hame10"L3>10"8411;
10# 386        if proc_();
10# 387        int 10" 322<>mttry">a>-&wref=h>-&wrss="com="esizc_lnfo NULL+cof">mwrigcvmtNULLRT_S">mwrigcvock<=a>);
10" 326Lass="comment"x */
10" 359     gtref="_unregister="+coiov_basenfref="_unregisterRT_SYiov_lenicd<, &p;&aa>o" cref="="+coiov_basenfT" c+coioRT_S">mwrigcvock<=a>);
10" 359     gtbdi_de>mtoyref="+codROFSmtbdi_de>mtoyRT_SYiov_lenicd<, &p;&aa>o" cbdi_unmappable="+coiov_basenfT" cbdi_unmappablelass">mwrigcvock<=a>);
10"esizc_lnfo bdi_de>mtoyref="+codROFSmtbdi_de>mtoyRT_SYiov_lenicd<, &p;&aa>o" cbdi_ro_mappable="+coiov_basenfT" cbdi_ro_mappablelass">mwrigcvock<=a>);
mtd_d2vtype216ame10"L3>10"esizc_lnfo bdi_de>mtoyref="+codROFSmtbdi_de>mtoyRT_SYiov_lenicd<, &p;&aa>o" cbdi_rw_mappable="+coiov_basenfT" cbdi_rw_mappablelass">mwrigcvock<=a>);
10"8411;
oodule_in  ="+coiov_basenfTodule_in  RT_SY">mtd_info in  _(mwrigcvock<=a>);
mtd_info cleanup_(mwrigcvock<=a>);
10# 411;
10" 322<>mttry">a>-&wrGPLa>-&wrss="com">mwrigcvock<=a>);
10" 322<>mttry">a>-&wrDavocaWoodhouse <dwmw2@infler ad.org="sr=>-&wrss="com">mwrigcvock<=a>);
10" 322<>mttry">a>-&wrCdri"commregistrtil thu of in casomment"s=>-&wrss="com">mwrigcvock<=a>);



Theassiginal LXR softwar sbytd powrigcvockhttp://sourceforge.net/proescrs/lxr=>LXR /a><9;s yRT_Ss=mmentexper ); l thbytwrigcvockmailto:lxr@10"ux.no">lxr@10"ux.noRT_S.
lxr.10"ux.no kindly hotire bytwrigcvockhttp://www.redpill-10"pro.no">Redpill L0"pro ASRT_Ss=provoclloof L0"ux="sreulswappu ofophrreduce ser "tte since 1995.