[PATCH 1/2] MTD: pass driver methods through partition wrappers on unpartitioned devices

Mike Dunn mikedunn at newsguy.com
Tue Dec 20 13:42:15 EST 2011


Ensure driver methods always go through the wrapper functions in the
partitioning code by creating a single "partition" on otherwise unpartitioned
devices.

Tested with nandsim, onenand_sim, and the diskonchip g4 nand driver (currently
out-of-tree).

Signed-off-by: Mike Dunn <mikedunn at newsguy.com>
---
 drivers/mtd/mtdcore.c |   17 ++++++++++++++---
 drivers/mtd/mtdpart.c |   10 +++++++---
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index b01993e..4ac1962 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -477,10 +477,21 @@ int mtd_device_parse_register(struct mtd_info *mtd, const char **types,
 	if (err > 0) {
 		err = add_mtd_partitions(mtd, real_parts, err);
 		kfree(real_parts);
+
 	} else if (err == 0) {
-		err = add_mtd_device(mtd);
-		if (err == 1)
-			err = -ENODEV;
+		/*
+		 * For unpartitioned devices, create a single "partition" that
+		 * spans the entire device, so that driver methods go through
+		 * partition wrappers in all cases.
+		 */
+		struct mtd_partition single_part = {
+			.name = (char *)mtd->name,
+			.offset = 0,
+			.size = mtd->size,
+			.mask_flags = 0,
+			.ecclayout = mtd->ecclayout,
+		};
+		err = add_mtd_partitions(mtd, &single_part, 1);
 	}
 
 	return err;
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index a0bd2de..81975a5 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -665,9 +665,11 @@ int add_mtd_partitions(struct mtd_info *master,
 {
 	struct mtd_part *slave;
 	uint64_t cur_offset = 0;
-	int i;
+	int i, ret;
 
-	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
+	if (nbparts > 1)
+		printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
+		       nbparts, master->name);
 
 	for (i = 0; i < nbparts; i++) {
 		slave = allocate_partition(master, parts + i, i, cur_offset);
@@ -678,7 +680,9 @@ int add_mtd_partitions(struct mtd_info *master,
 		list_add(&slave->list, &mtd_partitions);
 		mutex_unlock(&mtd_partitions_mutex);
 
-		add_mtd_device(&slave->mtd);
+		ret = add_mtd_device(&slave->mtd);
+		if (ret == 1)
+			return -ENODEV;
 
 		cur_offset = slave->offset + slave->mtd.size;
 	}
-- 
1.7.3.4




More information about the linux-mtd mailing list