mtd: Introduce and use iteration macro for reading the MTD device table

Signed-off-by: Ben Hutchings <bhutchings@solarflare.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index a33251f..e2f93a3 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -9,3 +9,18 @@
 
 extern struct mutex mtd_table_mutex;
 extern struct mtd_info *mtd_table[MAX_MTD_DEVICES];
+
+static inline struct mtd_info *__mtd_next_device(int i)
+{
+	while (i < MAX_MTD_DEVICES) {
+		if (mtd_table[i])
+			return mtd_table[i];
+		i++;
+	}
+	return NULL;
+}
+
+#define mtd_for_each_device(mtd)			\
+	for ((mtd) = __mtd_next_device(0);		\
+	     (mtd) != NULL;				\
+	     (mtd) = __mtd_next_device(mtd->index + 1))