Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / drivers / mtd / mtdconcat.c
index da4c9c1..880580c 100644 (file)
@@ -19,6 +19,8 @@
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/concat.h>
 
+#include <asm/div64.h>
+
 /*
  * Our storage structure:
  * Subdev points to an array of pointers to struct mtd_info objects
@@ -54,7 +56,7 @@ concat_read(struct mtd_info *mtd, loff_t from, size_t len,
            size_t * retlen, u_char * buf)
 {
        struct mtd_concat *concat = CONCAT(mtd);
-       int err = -EINVAL;
+       int ret = 0, err;
        int i;
 
        *retlen = 0;
@@ -78,19 +80,29 @@ concat_read(struct mtd_info *mtd, loff_t from, size_t len,
 
                err = subdev->read(subdev, from, size, &retsize, buf);
 
-               if (err)
-                       break;
+               /* Save information about bitflips! */
+               if (unlikely(err)) {
+                       if (err == -EBADMSG) {
+                               mtd->ecc_stats.failed++;
+                               ret = err;
+                       } else if (err == -EUCLEAN) {
+                               mtd->ecc_stats.corrected++;
+                               /* Do not overwrite -EBADMSG !! */
+                               if (!ret)
+                                       ret = err;
+                       } else
+                               return err;
+               }
 
                *retlen += retsize;
                len -= size;
                if (len == 0)
-                       break;
+                       return ret;
 
-               err = -EINVAL;
                buf += size;
                from = 0;
        }
-       return err;
+       return -EINVAL;
 }
 
 static int
@@ -141,119 +153,8 @@ concat_write(struct mtd_info *mtd, loff_t to, size_t len,
 }
 
 static int
-concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
-               size_t * retlen, u_char * buf, u_char * eccbuf,
-               struct nand_oobinfo *oobsel)
-{
-       struct mtd_concat *concat = CONCAT(mtd);
-       int err = -EINVAL;
-       int i;
-
-       *retlen = 0;
-
-       for (i = 0; i < concat->num_subdev; i++) {
-               struct mtd_info *subdev = concat->subdev[i];
-               size_t size, retsize;
-
-               if (from >= subdev->size) {
-                       /* Not destined for this subdev */
-                       size = 0;
-                       from -= subdev->size;
-                       continue;
-               }
-
-               if (from + len > subdev->size)
-                       /* First part goes into this subdev */
-                       size = subdev->size - from;
-               else
-                       /* Entire transaction goes into this subdev */
-                       size = len;
-
-               if (subdev->read_ecc)
-                       err = subdev->read_ecc(subdev, from, size,
-                                              &retsize, buf, eccbuf, oobsel);
-               else
-                       err = -EINVAL;
-
-               if (err)
-                       break;
-
-               *retlen += retsize;
-               len -= size;
-               if (len == 0)
-                       break;
-
-               err = -EINVAL;
-               buf += size;
-               if (eccbuf) {
-                       eccbuf += subdev->oobsize;
-                       /* in nand.c at least, eccbufs are
-                          tagged with 2 (int)eccstatus'; we
-                          must account for these */
-                       eccbuf += 2 * (sizeof (int));
-               }
-               from = 0;
-       }
-       return err;
-}
-
-static int
-concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
-                size_t * retlen, const u_char * buf, u_char * eccbuf,
-                struct nand_oobinfo *oobsel)
-{
-       struct mtd_concat *concat = CONCAT(mtd);
-       int err = -EINVAL;
-       int i;
-
-       if (!(mtd->flags & MTD_WRITEABLE))
-               return -EROFS;
-
-       *retlen = 0;
-
-       for (i = 0; i < concat->num_subdev; i++) {
-               struct mtd_info *subdev = concat->subdev[i];
-               size_t size, retsize;
-
-               if (to >= subdev->size) {
-                       size = 0;
-                       to -= subdev->size;
-                       continue;
-               }
-               if (to + len > subdev->size)
-                       size = subdev->size - to;
-               else
-                       size = len;
-
-               if (!(subdev->flags & MTD_WRITEABLE))
-                       err = -EROFS;
-               else if (subdev->write_ecc)
-                       err = subdev->write_ecc(subdev, to, size,
-                                               &retsize, buf, eccbuf, oobsel);
-               else
-                       err = -EINVAL;
-
-               if (err)
-                       break;
-
-               *retlen += retsize;
-               len -= size;
-               if (len == 0)
-                       break;
-
-               err = -EINVAL;
-               buf += size;
-               if (eccbuf)
-                       eccbuf += subdev->oobsize;
-               to = 0;
-       }
-       return err;
-}
-
-static int
-concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
-               unsigned long count, loff_t to, size_t * retlen,
-               u_char *eccbuf, struct nand_oobinfo *oobsel)
+concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
+               unsigned long count, loff_t to, size_t * retlen)
 {
        struct mtd_concat *concat = CONCAT(mtd);
        struct kvec *vecs_copy;
@@ -276,9 +177,11 @@ concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
                return -EINVAL;
 
        /* Check alignment */
-       if (mtd->writesize > 1)
-               if ((to % mtd->writesize) || (total_len % mtd->writesize))
+       if (mtd->writesize > 1) {
+               loff_t __to = to;
+               if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
                        return -EINVAL;
+       }
 
        /* make a copy of vecs */
        vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
@@ -311,10 +214,6 @@ concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
 
                if (!(subdev->flags & MTD_WRITEABLE))
                        err = -EROFS;
-               else if (eccbuf)
-                       err = subdev->writev_ecc(subdev, &vecs_copy[entry_low],
-                               entry_high - entry_low + 1, to, &retsize,
-                               eccbuf, oobsel);
                else
                        err = subdev->writev(subdev, &vecs_copy[entry_low],
                                entry_high - entry_low + 1, to, &retsize);
@@ -329,8 +228,6 @@ concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
 
                *retlen += retsize;
                total_len -= wsize;
-               if (concat->mtd.type == MTD_NANDFLASH && eccbuf)
-                       eccbuf += mtd->oobavail * (wsize / mtd->oobblock);
 
                if (total_len == 0)
                        break;
@@ -344,108 +241,106 @@ concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs,
 }
 
 static int
-concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
-               unsigned long count, loff_t to, size_t * retlen)
-{
-       return concat_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL);
-}
-
-static int
-concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
-               size_t * retlen, u_char * buf)
+concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
 {
        struct mtd_concat *concat = CONCAT(mtd);
-       int err = -EINVAL;
-       int i;
+       struct mtd_oob_ops devops = *ops;
+       int i, err, ret = 0;
 
-       *retlen = 0;
+       ops->retlen = ops->oobretlen = 0;
 
        for (i = 0; i < concat->num_subdev; i++) {
                struct mtd_info *subdev = concat->subdev[i];
-               size_t size, retsize;
 
                if (from >= subdev->size) {
-                       /* Not destined for this subdev */
-                       size = 0;
                        from -= subdev->size;
                        continue;
                }
-               if (from + len > subdev->size)
-                       /* First part goes into this subdev */
-                       size = subdev->size - from;
-               else
-                       /* Entire transaction goes into this subdev */
-                       size = len;
 
-               if (subdev->read_oob)
-                       err = subdev->read_oob(subdev, from, size,
-                                              &retsize, buf);
-               else
-                       err = -EINVAL;
-
-               if (err)
-                       break;
+               /* partial read ? */
+               if (from + devops.len > subdev->size)
+                       devops.len = subdev->size - from;
+
+               err = subdev->read_oob(subdev, from, &devops);
+               ops->retlen += devops.retlen;
+               ops->oobretlen += devops.oobretlen;
+
+               /* Save information about bitflips! */
+               if (unlikely(err)) {
+                       if (err == -EBADMSG) {
+                               mtd->ecc_stats.failed++;
+                               ret = err;
+                       } else if (err == -EUCLEAN) {
+                               mtd->ecc_stats.corrected++;
+                               /* Do not overwrite -EBADMSG !! */
+                               if (!ret)
+                                       ret = err;
+                       } else
+                               return err;
+               }
 
-               *retlen += retsize;
-               len -= size;
-               if (len == 0)
-                       break;
+               if (devops.datbuf) {
+                       devops.len = ops->len - ops->retlen;
+                       if (!devops.len)
+                               return ret;
+                       devops.datbuf += devops.retlen;
+               }
+               if (devops.oobbuf) {
+                       devops.ooblen = ops->ooblen - ops->oobretlen;
+                       if (!devops.ooblen)
+                               return ret;
+                       devops.oobbuf += ops->oobretlen;
+               }
 
-               err = -EINVAL;
-               buf += size;
                from = 0;
        }
-       return err;
+       return -EINVAL;
 }
 
 static int
-concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
-                size_t * retlen, const u_char * buf)
+concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
 {
        struct mtd_concat *concat = CONCAT(mtd);
-       int err = -EINVAL;
-       int i;
+       struct mtd_oob_ops devops = *ops;
+       int i, err;
 
        if (!(mtd->flags & MTD_WRITEABLE))
                return -EROFS;
 
-       *retlen = 0;
+       ops->retlen = 0;
 
        for (i = 0; i < concat->num_subdev; i++) {
                struct mtd_info *subdev = concat->subdev[i];
-               size_t size, retsize;
 
                if (to >= subdev->size) {
-                       size = 0;
                        to -= subdev->size;
                        continue;
                }
-               if (to + len > subdev->size)
-                       size = subdev->size - to;
-               else
-                       size = len;
 
-               if (!(subdev->flags & MTD_WRITEABLE))
-                       err = -EROFS;
-               else if (subdev->write_oob)
-                       err = subdev->write_oob(subdev, to, size, &retsize,
-                                               buf);
-               else
-                       err = -EINVAL;
+               /* partial write ? */
+               if (to + devops.len > subdev->size)
+                       devops.len = subdev->size - to;
 
+               err = subdev->write_oob(subdev, to, &devops);
+               ops->retlen += devops.retlen;
                if (err)
-                       break;
+                       return err;
 
-               *retlen += retsize;
-               len -= size;
-               if (len == 0)
-                       break;
-
-               err = -EINVAL;
-               buf += size;
+               if (devops.datbuf) {
+                       devops.len = ops->len - ops->retlen;
+                       if (!devops.len)
+                               return 0;
+                       devops.datbuf += devops.retlen;
+               }
+               if (devops.oobbuf) {
+                       devops.ooblen = ops->ooblen - ops->oobretlen;
+                       if (!devops.ooblen)
+                               return 0;
+                       devops.oobbuf += devops.oobretlen;
+               }
                to = 0;
        }
-       return err;
+       return -EINVAL;
 }
 
 static void concat_erase_callback(struct erase_info *instr)
@@ -782,6 +677,8 @@ static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
                }
 
                err = subdev->block_markbad(subdev, ofs);
+               if (!err)
+                       mtd->ecc_stats.badblocks++;
                break;
        }
 
@@ -811,14 +708,13 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],     /* subdevices to c
 
        /* allocate the device structure */
        size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
-       concat = kmalloc(size, GFP_KERNEL);
+       concat = kzalloc(size, GFP_KERNEL);
        if (!concat) {
                printk
                    ("memory allocation error while creating concatenated device \"%s\"\n",
                     name);
                return NULL;
        }
-       memset(concat, 0, size);
        concat->subdev = (struct mtd_info **) (concat + 1);
 
        /*
@@ -829,18 +725,10 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],     /* subdevices to c
        concat->mtd.flags = subdev[0]->flags;
        concat->mtd.size = subdev[0]->size;
        concat->mtd.erasesize = subdev[0]->erasesize;
-       concat->mtd.oobblock = subdev[0]->oobblock;
+       concat->mtd.writesize = subdev[0]->writesize;
        concat->mtd.oobsize = subdev[0]->oobsize;
-       concat->mtd.ecctype = subdev[0]->ecctype;
-       concat->mtd.eccsize = subdev[0]->eccsize;
-       if (subdev[0]->read_ecc)
-               concat->mtd.read_ecc = concat_read_ecc;
-       if (subdev[0]->write_ecc)
-               concat->mtd.write_ecc = concat_write_ecc;
        if (subdev[0]->writev)
                concat->mtd.writev = concat_writev;
-       if (subdev[0]->writev_ecc)
-               concat->mtd.writev_ecc = concat_writev_ecc;
        if (subdev[0]->read_oob)
                concat->mtd.read_oob = concat_read_oob;
        if (subdev[0]->write_oob)
@@ -850,6 +738,8 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],       /* subdevices to c
        if (subdev[0]->block_markbad)
                concat->mtd.block_markbad = concat_block_markbad;
 
+       concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
+
        concat->subdev[0] = subdev[0];
 
        for (i = 1; i < num_devs; i++) {
@@ -877,12 +767,11 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],     /* subdevices to c
                                    subdev[i]->flags & MTD_WRITEABLE;
                }
                concat->mtd.size += subdev[i]->size;
-               if (concat->mtd.oobblock   !=  subdev[i]->oobblock ||
+               concat->mtd.ecc_stats.badblocks +=
+                       subdev[i]->ecc_stats.badblocks;
+               if (concat->mtd.writesize   !=  subdev[i]->writesize ||
+                   concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
                    concat->mtd.oobsize    !=  subdev[i]->oobsize ||
-                   concat->mtd.ecctype    !=  subdev[i]->ecctype ||
-                   concat->mtd.eccsize    !=  subdev[i]->eccsize ||
-                   !concat->mtd.read_ecc  != !subdev[i]->read_ecc ||
-                   !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
                    !concat->mtd.read_oob  != !subdev[i]->read_oob ||
                    !concat->mtd.write_oob != !subdev[i]->write_oob) {
                        kfree(concat);
@@ -894,9 +783,7 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],       /* subdevices to c
 
        }
 
-       if(concat->mtd.type == MTD_NANDFLASH)
-               memcpy(&concat->mtd.oobinfo, &subdev[0]->oobinfo,
-                       sizeof(struct nand_oobinfo));
+       concat->mtd.ecclayout = subdev[0]->ecclayout;
 
        concat->num_subdev = num_devs;
        concat->mtd.name = name;