mtd: Fallback to ->_read/write_oob() when ->_read/write() is missing
authorBoris Brezillon <boris.brezillon@free-electrons.com>
Thu, 16 Aug 2018 15:29:59 +0000 (17:29 +0200)
committerJagan Teki <jagan@amarulasolutions.com>
Thu, 20 Sep 2018 14:40:49 +0000 (20:10 +0530)
Some MTD sublayers/drivers are implementing ->_read/write_oob() and
provide dummy wrappers for their ->_read/write() implementations.
Let the core handle this case instead of duplicating the logic.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Acked-by: Robert Jarzmik <robert.jarzmik@free.fr>
Acked-by: Brian Norris <computersforpeace@gmail.com>
Reviewed-by: Miquel Raynal <miquel.raynal@free-electrons.com>
Tested-by: Ladislav Michl <ladis@linux-mips.org>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Reviewed-by: Jagan Teki <jagan@openedev.com>
drivers/mtd/mtdcore.c
drivers/mtd/mtdpart.c
drivers/mtd/nand/nand_base.c
drivers/mtd/onenand/onenand_base.c

index 6217be2352864d998b29038b0a5ef4f91390ad03..60ad28efd432ca62a91a99d21e9a9c86fef05d1b 100644 (file)
@@ -937,7 +937,20 @@ int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
         * representing the maximum number of bitflips that were corrected on
         * any one ecc region (if applicable; zero otherwise).
         */
-       ret_code = mtd->_read(mtd, from, len, retlen, buf);
+       if (mtd->_read) {
+               ret_code = mtd->_read(mtd, from, len, retlen, buf);
+       } else if (mtd->_read_oob) {
+               struct mtd_oob_ops ops = {
+                       .len = len,
+                       .datbuf = buf,
+               };
+
+               ret_code = mtd->_read_oob(mtd, from, &ops);
+               *retlen = ops.retlen;
+       } else {
+               return -ENOTSUPP;
+       }
+
        if (unlikely(ret_code < 0))
                return ret_code;
        if (mtd->ecc_strength == 0)
@@ -952,10 +965,24 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
        *retlen = 0;
        if (to < 0 || to > mtd->size || len > mtd->size - to)
                return -EINVAL;
-       if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
+       if ((!mtd->_write && !mtd->_write_oob) ||
+           !(mtd->flags & MTD_WRITEABLE))
                return -EROFS;
        if (!len)
                return 0;
+
+       if (!mtd->_write) {
+               struct mtd_oob_ops ops = {
+                       .len = len,
+                       .datbuf = (u8 *)buf,
+               };
+               int ret;
+
+               ret = mtd->_write_oob(mtd, to, &ops);
+               *retlen = ops.retlen;
+               return ret;
+       }
+
        return mtd->_write(mtd, to, len, retlen, buf);
 }
 EXPORT_SYMBOL_GPL(mtd_write);
index f87c962205371c6491b1e992f8961027355be12f..ccbb1757eab93c353a64699d2396986a121e0f74 100644 (file)
@@ -417,8 +417,10 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
        slave->mtd.dev.parent = master->dev.parent;
 #endif
 
-       slave->mtd._read = part_read;
-       slave->mtd._write = part_write;
+       if (master->_read)
+               slave->mtd._read = part_read;
+       if (master->_write)
+               slave->mtd._write = part_write;
 
        if (master->_panic_write)
                slave->mtd._panic_write = part_panic_write;
index 9094f857c1e88d44e19b3573dc44f7eb372eb7ba..92daebe120bffb496e0d6614a5921c8d3a75f358 100644 (file)
@@ -1863,33 +1863,6 @@ read_retry:
        return max_bitflips;
 }
 
-/**
- * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
- * @mtd: MTD device structure
- * @from: offset to read from
- * @len: number of bytes to read
- * @retlen: pointer to variable to store the number of read bytes
- * @buf: the databuffer to put data
- *
- * Get hold of the chip and call nand_do_read.
- */
-static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
-                    size_t *retlen, uint8_t *buf)
-{
-       struct mtd_oob_ops ops;
-       int ret;
-
-       nand_get_device(mtd, FL_READING);
-       memset(&ops, 0, sizeof(ops));
-       ops.len = len;
-       ops.datbuf = buf;
-       ops.mode = MTD_OPS_PLACE_OOB;
-       ret = nand_do_read_ops(mtd, from, &ops);
-       *retlen = ops.retlen;
-       nand_release_device(mtd);
-       return ret;
-}
-
 /**
  * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
  * @mtd: mtd info structure
@@ -2674,33 +2647,6 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
        return ret;
 }
 
-/**
- * nand_write - [MTD Interface] NAND write with ECC
- * @mtd: MTD device structure
- * @to: offset to write to
- * @len: number of bytes to write
- * @retlen: pointer to variable to store the number of written bytes
- * @buf: the data to write
- *
- * NAND write with ECC.
- */
-static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
-                         size_t *retlen, const uint8_t *buf)
-{
-       struct mtd_oob_ops ops;
-       int ret;
-
-       nand_get_device(mtd, FL_WRITING);
-       memset(&ops, 0, sizeof(ops));
-       ops.len = len;
-       ops.datbuf = (uint8_t *)buf;
-       ops.mode = MTD_OPS_PLACE_OOB;
-       ret = nand_do_write_ops(mtd, to, &ops);
-       *retlen = ops.retlen;
-       nand_release_device(mtd);
-       return ret;
-}
-
 /**
  * nand_do_write_oob - [MTD Interface] NAND write out-of-band
  * @mtd: MTD device structure
@@ -4620,8 +4566,6 @@ int nand_scan_tail(struct mtd_info *mtd)
        mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
                                                MTD_CAP_NANDFLASH;
        mtd->_erase = nand_erase;
-       mtd->_read = nand_read;
-       mtd->_write = nand_write;
        mtd->_panic_write = panic_nand_write;
        mtd->_read_oob = nand_read_oob;
        mtd->_write_oob = nand_write_oob;
index 86b1640357e87174dcbd346998288a1967e8e200..371e2ecaa70fd85302b3f65e3f0b475f0224aae3 100644 (file)
@@ -2656,8 +2656,6 @@ int onenand_probe(struct mtd_info *mtd)
 
        mtd->flags = MTD_CAP_NANDFLASH;
        mtd->_erase = onenand_erase;
-       mtd->_read = onenand_read;
-       mtd->_write = onenand_write;
        mtd->_read_oob = onenand_read_oob;
        mtd->_write_oob = onenand_write_oob;
        mtd->_sync = onenand_sync;