summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/core.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2015-07-03 08:37:52 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2015-07-03 08:37:52 +0200
commit7b4e61bbf9462b38b496f8e151052c3081125aa4 (patch)
treea3da6659cf5b8745292ef7f2927b17a4deb05b17 /drivers/mtd/core.c
parentf2a2894c27db8f5785bfc7fbaddce8873c16661f (diff)
parent221033e217e3a9778d81088801fae45e986ff2e8 (diff)
downloadbarebox-7b4e61bbf9462b38b496f8e151052c3081125aa4.tar.gz
barebox-7b4e61bbf9462b38b496f8e151052c3081125aa4.tar.xz
Merge branch 'for-next/mtd'
Conflicts: drivers/mtd/core.c
Diffstat (limited to 'drivers/mtd/core.c')
-rw-r--r--drivers/mtd/core.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 8b8254c2d3..fda903441c 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -18,7 +18,9 @@
#include <common.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/mtd.h>
+#include <mtd/ubi-user.h>
#include <cmdlinepart.h>
+#include <filetype.h>
#include <init.h>
#include <xfuncs.h>
#include <driver.h>
@@ -542,6 +544,41 @@ static int of_mtd_fixup(struct device_node *root, void *ctx)
return 0;
}
+static int mtd_detect(struct device_d *dev)
+{
+ struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
+ int bufsize = 512;
+ void *buf;
+ int ret;
+ enum filetype filetype;
+ size_t retlen;
+
+ /*
+ * Do not try to attach an UBI device if this device has partitions
+ * as it's not a good idea to attach UBI on a raw device when the
+ * real UBI only spans the first partition.
+ */
+ if (!list_empty(&mtd->partitions))
+ return -EBUSY;
+
+ buf = xmalloc(bufsize);
+
+ ret = mtd_read(mtd, 0, bufsize, &retlen, buf);
+ if (ret)
+ goto out;
+
+ filetype = file_detect_type(buf, bufsize);
+ if (filetype == filetype_ubi) {
+ ret = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 20);
+ if (ret == -EEXIST)
+ ret = 0;
+ }
+out:
+ free(buf);
+
+ return ret;
+}
+
int add_mtd_device(struct mtd_info *mtd, const char *devname, int device_id)
{
struct mtddev_hook *hook;
@@ -554,6 +591,9 @@ int add_mtd_device(struct mtd_info *mtd, const char *devname, int device_id)
if (mtd->parent)
mtd->class_dev.parent = mtd->parent;
+ if (IS_ENABLED(CONFIG_MTD_UBI))
+ mtd->class_dev.detect = mtd_detect;
+
ret = register_device(&mtd->class_dev);
if (ret)
return ret;