This refactors the code so that we get a mtd_add_bb which can
be used to create a bb dev on a mtd_info.

Signed-off-by: Sascha Hauer <[email protected]>
---
 drivers/mtd/nand/nand-bb.c | 53 +++++++++++++++++++++++++++-------------------
 include/nand.h             |  6 ++++++
 2 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/drivers/mtd/nand/nand-bb.c b/drivers/mtd/nand/nand-bb.c
index 06b5824..0d3de41 100644
--- a/drivers/mtd/nand/nand-bb.c
+++ b/drivers/mtd/nand/nand-bb.c
@@ -29,6 +29,7 @@
 #include <fcntl.h>
 #include <libgen.h>
 #include <linux/list.h>
+#include <linux/err.h>
 
 struct nand_bb {
        char *name;
@@ -240,32 +241,18 @@ static struct file_operations nand_bb_ops = {
 
 static LIST_HEAD(bb_list);
 
-/**
- * Add a bad block aware device ontop of another (NAND) device
- * @param[in] dev The device to add a partition on
- * @param[in] name Partition name (can be obtained with devinfo command)
- * @return The device representing the new partition.
- */
-int dev_add_bb_dev(const char *path, const char *name)
+struct cdev *mtd_add_bb(struct mtd_info *mtd, const char *name)
 {
        struct nand_bb *bb;
-       struct cdev *parent;
-       int ret = -ENOMEM;
-
-       parent = cdev_by_name(path);
-       if (!parent)
-               return -ENODEV;
-
-       if (!parent->mtd)
-               return -EINVAL;
+       int ret;
 
        bb = xzalloc(sizeof(*bb));
-       bb->mtd = parent->mtd;
+       bb->mtd = mtd;
 
        if (name)
                bb->cdev.name = xstrdup(name);
        else
-               bb->cdev.name = asprintf("%s.bb", path);;
+               bb->cdev.name = asprintf("%s.bb", mtd->cdev.name);
 
        nand_bb_calc_size(bb);
        bb->cdev.ops = &nand_bb_ops;
@@ -273,15 +260,37 @@ int dev_add_bb_dev(const char *path, const char *name)
 
        ret = devfs_create(&bb->cdev);
        if (ret)
-               goto out4;
+               goto err;
 
        list_add_tail(&bb->list, &bb_list);
 
-       return 0;
+       return &bb->cdev;
 
-out4:
+err:
        free(bb);
-       return ret;
+       return ERR_PTR(ret);
+}
+
+/**
+ * Add a bad block aware device ontop of another (NAND) device
+ * @param[in] dev The device to add a partition on
+ * @param[in] name Partition name (can be obtained with devinfo command)
+ * @return The device representing the new partition.
+ */
+int dev_add_bb_dev(const char *path, const char *name)
+{
+       struct cdev *parent, *cdev;
+
+       parent = cdev_by_name(path);
+       if (!parent)
+               return -ENODEV;
+
+       if (!parent->mtd)
+               return -EINVAL;
+
+       cdev = mtd_add_bb(parent->mtd, name);
+
+       return PTR_ERR(cdev);
 }
 
 int dev_remove_bb_dev(const char *name)
diff --git a/include/nand.h b/include/nand.h
index a0e77cc..1da35d0 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -7,6 +7,7 @@ struct nand_bb;
 #ifdef CONFIG_NAND
 int dev_add_bb_dev(const char *filename, const char *name);
 int dev_remove_bb_dev(const char *name);
+struct cdev *mtd_add_bb(struct mtd_info *mtd, const char *name);
 #else
 static inline int dev_add_bb_dev(const char *filename, const char *name) {
        return 0;
@@ -15,6 +16,11 @@ static inline int dev_remove_bb_dev(const char *name)
 {
        return 0;
 }
+
+static inline struct cdev *mtd_add_bb(struct mtd_info *mtd, const char *name)
+{
+       return NULL;
+}
 #endif
 
 #endif /* __NAND_H__ */
-- 
1.9.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to