Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c2b78452 authored by Boris Brezillon's avatar Boris Brezillon
Browse files

mtd: use mtd_ooblayout_xxx() helpers where appropriate



The mtd_ooblayout_xxx() helper functions have been added to avoid direct
accesses to the ecclayout field, and thus ease for future reworks.
Use these helpers in all places where the oobfree[] and eccpos[] arrays
where directly accessed.

Signed-off-by: default avatarBoris Brezillon <boris.brezillon@free-electrons.com>
parent 75eb2cec
Loading
Loading
Loading
Loading
+88 −19
Original line number Original line Diff line number Diff line
@@ -472,31 +472,104 @@ static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
 * nand_ecclayout flexibly (i.e. the struct may change size in new
 * nand_ecclayout flexibly (i.e. the struct may change size in new
 * releases without requiring major rewrites).
 * releases without requiring major rewrites).
 */
 */
static int shrink_ecclayout(const struct nand_ecclayout *from,
static int shrink_ecclayout(struct mtd_info *mtd,
			    struct nand_ecclayout_user *to)
			    struct nand_ecclayout_user *to)
{
{
	int i;
	struct mtd_oob_region oobregion;
	int i, section = 0, ret;


	if (!from || !to)
	if (!mtd || !to)
		return -EINVAL;
		return -EINVAL;


	memset(to, 0, sizeof(*to));
	memset(to, 0, sizeof(*to));


	to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
	to->eccbytes = 0;
	for (i = 0; i < to->eccbytes; i++)
	for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
		to->eccpos[i] = from->eccpos[i];
		u32 eccpos;

		ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
		if (ret < 0) {
			if (ret != -ERANGE)
				return ret;

			break;
		}

		eccpos = oobregion.offset;
		for (; i < MTD_MAX_ECCPOS_ENTRIES &&
		       eccpos < oobregion.offset + oobregion.length; i++) {
			to->eccpos[i] = eccpos++;
			to->eccbytes++;
		}
	}


	for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
	for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
		if (from->oobfree[i].length == 0 &&
		ret = mtd_ooblayout_free(mtd, i, &oobregion);
				from->oobfree[i].offset == 0)
		if (ret < 0) {
			if (ret != -ERANGE)
				return ret;

			break;
			break;
		to->oobavail += from->oobfree[i].length;
		}
		to->oobfree[i] = from->oobfree[i];

		to->oobfree[i].offset = oobregion.offset;
		to->oobfree[i].length = oobregion.length;
		to->oobavail += to->oobfree[i].length;
	}
	}


	return 0;
	return 0;
}
}


static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
{
	struct mtd_oob_region oobregion;
	int i, section = 0, ret;

	if (!mtd || !to)
		return -EINVAL;

	memset(to, 0, sizeof(*to));

	to->eccbytes = 0;
	for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
		u32 eccpos;

		ret = mtd_ooblayout_ecc(mtd, section, &oobregion);
		if (ret < 0) {
			if (ret != -ERANGE)
				return ret;

			break;
		}

		if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
			return -EINVAL;

		eccpos = oobregion.offset;
		for (; eccpos < oobregion.offset + oobregion.length; i++) {
			to->eccpos[i] = eccpos++;
			to->eccbytes++;
		}
	}

	for (i = 0; i < 8; i++) {
		ret = mtd_ooblayout_free(mtd, i, &oobregion);
		if (ret < 0) {
			if (ret != -ERANGE)
				return ret;

			break;
		}

		to->oobfree[i][0] = oobregion.offset;
		to->oobfree[i][1] = oobregion.length;
	}

	to->useecc = MTD_NANDECC_AUTOPLACE;

	return 0;
}

static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
			       struct blkpg_ioctl_arg *arg)
			       struct blkpg_ioctl_arg *arg)
{
{
@@ -817,14 +890,10 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)


		if (!mtd->ecclayout)
		if (!mtd->ecclayout)
			return -EOPNOTSUPP;
			return -EOPNOTSUPP;
		if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
			return -EINVAL;


		oi.useecc = MTD_NANDECC_AUTOPLACE;
		ret = get_oobinfo(mtd, &oi);
		memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
		if (ret)
		memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
			return ret;
		       sizeof(oi.oobfree));
		oi.eccbytes = mtd->ecclayout->eccbytes;


		if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
		if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
			return -EFAULT;
			return -EFAULT;
@@ -920,7 +989,7 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
		if (!usrlay)
		if (!usrlay)
			return -ENOMEM;
			return -ENOMEM;


		shrink_ecclayout(mtd->ecclayout, usrlay);
		shrink_ecclayout(mtd, usrlay);


		if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
		if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
			ret = -EFAULT;
			ret = -EFAULT;