mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* nand patches
@ 2011-04-11 14:11 Sascha Hauer
  2011-04-11 14:11 ` [PATCH 1/4] nand: nand_block_markbad is only used with nand write support Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2011-04-11 14:11 UTC (permalink / raw)
  To: barebox

Some more nand cleanup. The badblock device code is moved from
the nand command to drivers/mtd to make it work without the command.

Sascha Hauer (4):
      nand: nand_block_markbad is only used with nand write support
      nand: move bb handling code to drivers/mtd/nand
      nand bb: switch to cdev operations
      nand bb: add proper bb remove function

 commands/nand.c              |  263 +-------------------------------------
 drivers/mtd/nand/Makefile    |    2 +-
 drivers/mtd/nand/nand-bb.c   |  291 ++++++++++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/nand.c      |    2 +
 drivers/mtd/nand/nand.h      |    1 +
 drivers/mtd/nand/nand_base.c |   23 +---
 include/nand.h               |    5 +
 7 files changed, 304 insertions(+), 283 deletions(-)
 create mode 100644 drivers/mtd/nand/nand-bb.c

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] nand: nand_block_markbad is only used with nand write support
  2011-04-11 14:11 nand patches Sascha Hauer
@ 2011-04-11 14:11 ` Sascha Hauer
  2011-04-11 14:11 ` [PATCH 2/4] nand: move bb handling code to drivers/mtd/nand Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2011-04-11 14:11 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/nand/nand.c      |    2 ++
 drivers/mtd/nand/nand.h      |    1 +
 drivers/mtd/nand/nand_base.c |   23 ++---------------------
 3 files changed, 5 insertions(+), 21 deletions(-)

diff --git a/drivers/mtd/nand/nand.c b/drivers/mtd/nand/nand.c
index deb9400..9423ac8 100644
--- a/drivers/mtd/nand/nand.c
+++ b/drivers/mtd/nand/nand.c
@@ -116,9 +116,11 @@ static int nand_ioctl(struct cdev *cdev, int request, void *buf)
 	case MEMGETBADBLOCK:
 		debug("MEMGETBADBLOCK: 0x%08lx\n", (off_t)buf);
 		return info->block_isbad(info, (off_t)buf);
+#ifdef CONFIG_NAND_WRITE
 	case MEMSETBADBLOCK:
 		debug("MEMSETBADBLOCK: 0x%08lx\n", (off_t)buf);
 		return info->block_markbad(info, (off_t)buf);
+#endif
 	case MEMGETINFO:
 		user->type	= info->type;
 		user->flags	= info->flags;
diff --git a/drivers/mtd/nand/nand.h b/drivers/mtd/nand/nand.h
index 15bed4e..123258d 100644
--- a/drivers/mtd/nand/nand.h
+++ b/drivers/mtd/nand/nand.h
@@ -9,6 +9,7 @@ int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs);
 int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
 			       int allowbbt);
 int nand_block_isbad(struct mtd_info *mtd, loff_t offs);
+int nand_block_markbad(struct mtd_info *mtd, loff_t ofs);
 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len);
 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len);
 void single_erase_cmd(struct mtd_info *mtd, int page);
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index d627f16..5e122a1 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -981,26 +981,6 @@ int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
 	return nand_block_checkbad(mtd, offs, 1, 0);
 }
 
-/**
- * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
- * @mtd:	MTD device structure
- * @ofs:	offset relative to mtd start
- */
-static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
-{
-	struct nand_chip *chip = mtd->priv;
-	int ret;
-
-	if ((ret = nand_block_isbad(mtd, ofs))) {
-		/* If it was bad already, return success and do nothing. */
-		if (ret > 0)
-			return 0;
-		return ret;
-	}
-
-	return chip->block_markbad(mtd, ofs);
-}
-
 /*
  * Set default functions
  */
@@ -1431,8 +1411,9 @@ int nand_scan_tail(struct mtd_info *mtd)
 	mtd->lock = NULL;
 	mtd->unlock = NULL;
 	mtd->block_isbad = nand_block_isbad;
+#ifdef CONFIG_NAND_WRITE
 	mtd->block_markbad = nand_block_markbad;
-
+#endif
 	/* propagate ecc.layout to mtd_info */
 	mtd->ecclayout = chip->ecc.layout;
 
-- 
1.7.2.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] nand: move bb handling code to drivers/mtd/nand
  2011-04-11 14:11 nand patches Sascha Hauer
  2011-04-11 14:11 ` [PATCH 1/4] nand: nand_block_markbad is only used with nand write support Sascha Hauer
@ 2011-04-11 14:11 ` Sascha Hauer
  2011-04-11 14:11 ` [PATCH 3/4] nand bb: switch to cdev operations Sascha Hauer
  2011-04-11 14:11 ` [PATCH 4/4] nand bb: add proper bb remove function Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2011-04-11 14:11 UTC (permalink / raw)
  To: barebox

It's good to seperate the code which others can use from commands.
This way other users do not depend on the command being compiled in.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/nand.c            |  248 --------------------------------------
 drivers/mtd/nand/Makefile  |    2 +-
 drivers/mtd/nand/nand-bb.c |  282 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 283 insertions(+), 249 deletions(-)
 create mode 100644 drivers/mtd/nand/nand-bb.c

diff --git a/commands/nand.c b/commands/nand.c
index dc8fd9b..9a72780 100644
--- a/commands/nand.c
+++ b/commands/nand.c
@@ -32,254 +32,6 @@
 #include <fcntl.h>
 #include <libgen.h>
 
-struct nand_bb {
-	char *devname;
-	char *name;
-	int open;
-	int needs_write;
-
-	struct mtd_info_user info;
-
-	size_t raw_size;
-	size_t size;
-	int fd;
-	off_t offset;
-	void *writebuf;
-
-	struct cdev cdev;
-};
-
-static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
-	unsigned long offset, ulong flags)
-{
-	struct nand_bb *bb = cdev->priv;
-	int ret, bytes = 0, now;
-
-	debug("%s %d %d\n", __func__, offset, count);
-
-	while(count) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)bb->offset);
-		if (ret < 0)
-			return ret;
-
-		if (ret) {
-			printf("skipping bad block at 0x%08lx\n", bb->offset);
-			bb->offset += bb->info.erasesize;
-			continue;
-		}
-
-		now = min(count, (size_t)(bb->info.erasesize -
-				(bb->offset % bb->info.erasesize)));
-		lseek(bb->fd, bb->offset, SEEK_SET);
-		ret = read(bb->fd, buf, now);
-		if (ret < 0)
-			return ret;
-		buf += now;
-		count -= now;
-		bb->offset += now;
-		bytes += now;
-	};
-
-	return bytes;
-}
-
-/* Must be a multiple of the largest NAND page size */
-#define BB_WRITEBUF_SIZE	4096
-
-#ifdef CONFIG_NAND_WRITE
-static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
-{
-	int ret, now;
-	void *buf = bb->writebuf;
-	int cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
-
-	while (count) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)cur_ofs);
-		if (ret < 0)
-			return ret;
-
-		if (ret) {
-			debug("skipping bad block at 0x%08x\n", cur_ofs);
-			bb->offset += bb->info.erasesize;
-			cur_ofs += bb->info.erasesize;
-			continue;
-		}
-
-		now = min(count, (size_t)(bb->info.erasesize));
-		lseek(bb->fd, cur_ofs, SEEK_SET);
-		ret = write(bb->fd, buf, now);
-		if (ret < 0)
-			return ret;
-		buf += now;
-		count -= now;
-		cur_ofs += now;
-	};
-
-	return 0;
-}
-
-static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
-	unsigned long offset, ulong flags)
-{
-	struct nand_bb *bb = cdev->priv;
-	int bytes = count, now, wroffs, ret;
-
-	debug("%s offset: 0x%08x count: 0x%08x\n", __func__, offset, count);
-
-	while (count) {
-		wroffs = bb->offset % BB_WRITEBUF_SIZE;
-		now = min((int)count, BB_WRITEBUF_SIZE - wroffs);
-		memcpy(bb->writebuf + wroffs, buf, now);
-
-		if (wroffs + now == BB_WRITEBUF_SIZE) {
-			bb->needs_write = 0;
-			ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE);
-			if (ret)
-				return ret;
-		} else {
-			bb->needs_write = 1;
-		}
-
-		bb->offset += now;
-		count -= now;
-		buf += now;
-	}
-
-	return bytes;
-}
-
-static int nand_bb_erase(struct cdev *cdev, size_t count, unsigned long offset)
-{
-	struct nand_bb *bb = cdev->priv;
-
-	if (offset != 0) {
-		printf("can only erase from beginning of device\n");
-		return -EINVAL;
-	}
-
-	lseek(bb->fd, 0, SEEK_SET);
-
-	return erase(bb->fd, bb->raw_size, 0);
-}
-#endif
-
-static int nand_bb_open(struct cdev *cdev)
-{
-	struct nand_bb *bb = cdev->priv;
-
-	if (bb->open)
-		return -EBUSY;
-
-	bb->open = 1;
-	bb->offset = 0;
-	bb->needs_write = 0;
-	bb->writebuf = xmalloc(BB_WRITEBUF_SIZE);
-
-	return 0;
-}
-
-static int nand_bb_close(struct cdev *cdev)
-{
-	struct nand_bb *bb = cdev->priv;
-
-#ifdef CONFIG_NAND_WRITE
-	if (bb->needs_write)
-		nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
-#endif
-	bb->open = 0;
-	free(bb->writebuf);
-
-	return 0;
-}
-
-static int nand_bb_calc_size(struct nand_bb *bb)
-{
-	ulong pos = 0;
-	int ret;
-
-	while (pos < bb->raw_size) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)pos);
-		if (ret < 0)
-			return ret;
-		if (!ret)
-			bb->cdev.size += bb->info.erasesize;
-
-		pos += bb->info.erasesize;
-	}
-
-	return 0;
-}
-
-static struct file_operations nand_bb_ops = {
-	.open   = nand_bb_open,
-	.close  = nand_bb_close,
-	.read  	= nand_bb_read,
-#ifdef CONFIG_NAND_WRITE
-	.write 	= nand_bb_write,
-	.erase	= nand_bb_erase,
-#endif
-};
-
-/**
- * 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(char *path, const char *name)
-{
-	struct nand_bb *bb;
-	int ret = -ENOMEM;
-	struct stat s;
-
-	bb = xzalloc(sizeof(*bb));
-	bb->devname = asprintf("/dev/%s", basename(path));
-	if (!bb->devname)
-		goto out1;
-
-	if (name)
-		bb->cdev.name = strdup(name);
-	else
-		bb->cdev.name = asprintf("%s.bb", basename(path));
-
-	if (!bb->cdev.name)
-		goto out2;
-
-	ret = stat(bb->devname, &s);
-	if (ret)
-		goto out3;
-
-	bb->raw_size = s.st_size;
-
-	bb->fd = open(bb->devname, O_RDWR);
-	if (bb->fd < 0) {
-		ret = -ENODEV;
-		goto out3;
-	}
-
-	ret = ioctl(bb->fd, MEMGETINFO, &bb->info);
-	if (ret)
-		goto out4;
-
-	nand_bb_calc_size(bb);
-	bb->cdev.ops = &nand_bb_ops;
-	bb->cdev.priv = bb;
-
-	devfs_create(&bb->cdev);
-
-	return 0;
-
-out4:
-	close(bb->fd);
-out3:
-	free(bb->cdev.name);
-out2:
-	free(bb->devname);
-out1:
-	free(bb);
-	return ret;
-}
-
 #define NAND_ADD (1 << 0)
 #define NAND_DEL (1 << 1)
 #define NAND_MARKBAD (1 << 2)
diff --git a/drivers/mtd/nand/Makefile b/drivers/mtd/nand/Makefile
index 3a31bc1..149cbbf 100644
--- a/drivers/mtd/nand/Makefile
+++ b/drivers/mtd/nand/Makefile
@@ -6,7 +6,7 @@ obj-$(CONFIG_NAND_ECC_SOFT)		+= nand_ecc.o nand_swecc.o
 obj-$(CONFIG_NAND_ECC_HW)		+= nand_hwecc.o
 obj-$(CONFIG_NAND_ECC_HW_SYNDROME)	+= nand_hwecc_syndrome.o
 obj-$(CONFIG_MTD_NAND_IDS)		+= nand_ids.o
-obj-$(CONFIG_NAND)			+= nand_base.o
+obj-$(CONFIG_NAND)			+= nand_base.o nand-bb.o
 obj-$(CONFIG_NAND_BBT)			+= nand_bbt.o
 
 obj-$(CONFIG_MTD_NAND_DISKONCHIP)	+= diskonchip.o
diff --git a/drivers/mtd/nand/nand-bb.c b/drivers/mtd/nand/nand-bb.c
new file mode 100644
index 0000000..233e974
--- /dev/null
+++ b/drivers/mtd/nand/nand-bb.c
@@ -0,0 +1,282 @@
+/*
+ * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <linux/stat.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <xfuncs.h>
+#include <init.h>
+#include <ioctl.h>
+#include <nand.h>
+#include <linux/mtd/mtd-abi.h>
+#include <fcntl.h>
+#include <libgen.h>
+
+struct nand_bb {
+	char *devname;
+	char *name;
+	int open;
+	int needs_write;
+
+	struct mtd_info_user info;
+
+	size_t raw_size;
+	size_t size;
+	int fd;
+	off_t offset;
+	void *writebuf;
+
+	struct cdev cdev;
+};
+
+static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
+	unsigned long offset, ulong flags)
+{
+	struct nand_bb *bb = cdev->priv;
+	int ret, bytes = 0, now;
+
+	debug("%s %d %d\n", __func__, offset, count);
+
+	while(count) {
+		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)bb->offset);
+		if (ret < 0)
+			return ret;
+
+		if (ret) {
+			printf("skipping bad block at 0x%08lx\n", bb->offset);
+			bb->offset += bb->info.erasesize;
+			continue;
+		}
+
+		now = min(count, (size_t)(bb->info.erasesize -
+				(bb->offset % bb->info.erasesize)));
+		lseek(bb->fd, bb->offset, SEEK_SET);
+		ret = read(bb->fd, buf, now);
+		if (ret < 0)
+			return ret;
+		buf += now;
+		count -= now;
+		bb->offset += now;
+		bytes += now;
+	};
+
+	return bytes;
+}
+
+/* Must be a multiple of the largest NAND page size */
+#define BB_WRITEBUF_SIZE	4096
+
+#ifdef CONFIG_NAND_WRITE
+static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
+{
+	int ret, now;
+	void *buf = bb->writebuf;
+	int cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
+
+	while (count) {
+		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)cur_ofs);
+		if (ret < 0)
+			return ret;
+
+		if (ret) {
+			debug("skipping bad block at 0x%08x\n", cur_ofs);
+			bb->offset += bb->info.erasesize;
+			cur_ofs += bb->info.erasesize;
+			continue;
+		}
+
+		now = min(count, (size_t)(bb->info.erasesize));
+		lseek(bb->fd, cur_ofs, SEEK_SET);
+		ret = write(bb->fd, buf, now);
+		if (ret < 0)
+			return ret;
+		buf += now;
+		count -= now;
+		cur_ofs += now;
+	};
+
+	return 0;
+}
+
+static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
+	unsigned long offset, ulong flags)
+{
+	struct nand_bb *bb = cdev->priv;
+	int bytes = count, now, wroffs, ret;
+
+	debug("%s offset: 0x%08x count: 0x%08x\n", __func__, offset, count);
+
+	while (count) {
+		wroffs = bb->offset % BB_WRITEBUF_SIZE;
+		now = min((int)count, BB_WRITEBUF_SIZE - wroffs);
+		memcpy(bb->writebuf + wroffs, buf, now);
+
+		if (wroffs + now == BB_WRITEBUF_SIZE) {
+			bb->needs_write = 0;
+			ret = nand_bb_write_buf(bb, BB_WRITEBUF_SIZE);
+			if (ret)
+				return ret;
+		} else {
+			bb->needs_write = 1;
+		}
+
+		bb->offset += now;
+		count -= now;
+		buf += now;
+	}
+
+	return bytes;
+}
+
+static int nand_bb_erase(struct cdev *cdev, size_t count, unsigned long offset)
+{
+	struct nand_bb *bb = cdev->priv;
+
+	if (offset != 0) {
+		printf("can only erase from beginning of device\n");
+		return -EINVAL;
+	}
+
+	lseek(bb->fd, 0, SEEK_SET);
+
+	return erase(bb->fd, bb->raw_size, 0);
+}
+#endif
+
+static int nand_bb_open(struct cdev *cdev)
+{
+	struct nand_bb *bb = cdev->priv;
+
+	if (bb->open)
+		return -EBUSY;
+
+	bb->open = 1;
+	bb->offset = 0;
+	bb->needs_write = 0;
+	bb->writebuf = xmalloc(BB_WRITEBUF_SIZE);
+
+	return 0;
+}
+
+static int nand_bb_close(struct cdev *cdev)
+{
+	struct nand_bb *bb = cdev->priv;
+
+#ifdef CONFIG_NAND_WRITE
+	if (bb->needs_write)
+		nand_bb_write_buf(bb, bb->offset % BB_WRITEBUF_SIZE);
+#endif
+	bb->open = 0;
+	free(bb->writebuf);
+
+	return 0;
+}
+
+static int nand_bb_calc_size(struct nand_bb *bb)
+{
+	ulong pos = 0;
+	int ret;
+
+	while (pos < bb->raw_size) {
+		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)pos);
+		if (ret < 0)
+			return ret;
+		if (!ret)
+			bb->cdev.size += bb->info.erasesize;
+
+		pos += bb->info.erasesize;
+	}
+
+	return 0;
+}
+
+static struct file_operations nand_bb_ops = {
+	.open   = nand_bb_open,
+	.close  = nand_bb_close,
+	.read  	= nand_bb_read,
+#ifdef CONFIG_NAND_WRITE
+	.write 	= nand_bb_write,
+	.erase	= nand_bb_erase,
+#endif
+};
+
+/**
+ * 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(char *path, const char *name)
+{
+	struct nand_bb *bb;
+	int ret = -ENOMEM;
+	struct stat s;
+
+	bb = xzalloc(sizeof(*bb));
+	bb->devname = asprintf("/dev/%s", basename(path));
+	if (!bb->devname)
+		goto out1;
+
+	if (name)
+		bb->cdev.name = strdup(name);
+	else
+		bb->cdev.name = asprintf("%s.bb", basename(path));
+
+	if (!bb->cdev.name)
+		goto out2;
+
+	ret = stat(bb->devname, &s);
+	if (ret)
+		goto out3;
+
+	bb->raw_size = s.st_size;
+
+	bb->fd = open(bb->devname, O_RDWR);
+	if (bb->fd < 0) {
+		ret = -ENODEV;
+		goto out3;
+	}
+
+	ret = ioctl(bb->fd, MEMGETINFO, &bb->info);
+	if (ret)
+		goto out4;
+
+	nand_bb_calc_size(bb);
+	bb->cdev.ops = &nand_bb_ops;
+	bb->cdev.priv = bb;
+
+	devfs_create(&bb->cdev);
+
+	return 0;
+
+out4:
+	close(bb->fd);
+out3:
+	free(bb->cdev.name);
+out2:
+	free(bb->devname);
+out1:
+	free(bb);
+	return ret;
+}
+
-- 
1.7.2.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] nand bb: switch to cdev operations
  2011-04-11 14:11 nand patches Sascha Hauer
  2011-04-11 14:11 ` [PATCH 1/4] nand: nand_block_markbad is only used with nand write support Sascha Hauer
  2011-04-11 14:11 ` [PATCH 2/4] nand: move bb handling code to drivers/mtd/nand Sascha Hauer
@ 2011-04-11 14:11 ` Sascha Hauer
  2011-04-11 14:11 ` [PATCH 4/4] nand bb: add proper bb remove function Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2011-04-11 14:11 UTC (permalink / raw)
  To: barebox

The cdev operations are available without the complete file API,
so they are more suitable for internal usage.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/nand.c            |    2 +-
 drivers/mtd/nand/nand-bb.c |   65 +++++++++++++++++--------------------------
 2 files changed, 27 insertions(+), 40 deletions(-)

diff --git a/commands/nand.c b/commands/nand.c
index 9a72780..ed55625 100644
--- a/commands/nand.c
+++ b/commands/nand.c
@@ -63,7 +63,7 @@ static int do_nand(struct command *cmdtp, int argc, char *argv[])
 
 	if (command & NAND_ADD) {
 		while (optind < argc) {
-			if (dev_add_bb_dev(argv[optind], NULL))
+			if (dev_add_bb_dev(basename(argv[optind]), NULL))
 				return 1;
 
 			optind++;
diff --git a/drivers/mtd/nand/nand-bb.c b/drivers/mtd/nand/nand-bb.c
index 233e974..d71a2b0 100644
--- a/drivers/mtd/nand/nand-bb.c
+++ b/drivers/mtd/nand/nand-bb.c
@@ -33,7 +33,8 @@
 #include <libgen.h>
 
 struct nand_bb {
-	char *devname;
+	char cdevname[MAX_DRIVER_NAME];
+	struct cdev *cdev_parent;
 	char *name;
 	int open;
 	int needs_write;
@@ -42,7 +43,6 @@ struct nand_bb {
 
 	size_t raw_size;
 	size_t size;
-	int fd;
 	off_t offset;
 	void *writebuf;
 
@@ -53,12 +53,13 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
 	unsigned long offset, ulong flags)
 {
 	struct nand_bb *bb = cdev->priv;
+	struct cdev *parent = bb->cdev_parent;
 	int ret, bytes = 0, now;
 
 	debug("%s %d %d\n", __func__, offset, count);
 
 	while(count) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)bb->offset);
+		ret = cdev_ioctl(parent, MEMGETBADBLOCK, (void *)bb->offset);
 		if (ret < 0)
 			return ret;
 
@@ -70,8 +71,7 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
 
 		now = min(count, (size_t)(bb->info.erasesize -
 				(bb->offset % bb->info.erasesize)));
-		lseek(bb->fd, bb->offset, SEEK_SET);
-		ret = read(bb->fd, buf, now);
+		ret = cdev_read(parent, buf, now, bb->offset, 0);
 		if (ret < 0)
 			return ret;
 		buf += now;
@@ -90,11 +90,12 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
 static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
 {
 	int ret, now;
+	struct cdev *parent = bb->cdev_parent;
 	void *buf = bb->writebuf;
 	int cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
 
 	while (count) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)cur_ofs);
+		ret = cdev_ioctl(parent, MEMGETBADBLOCK, (void *)cur_ofs);
 		if (ret < 0)
 			return ret;
 
@@ -106,8 +107,7 @@ static int nand_bb_write_buf(struct nand_bb *bb, size_t count)
 		}
 
 		now = min(count, (size_t)(bb->info.erasesize));
-		lseek(bb->fd, cur_ofs, SEEK_SET);
-		ret = write(bb->fd, buf, now);
+		ret = cdev_write(parent, buf, now, cur_ofs, 0);
 		if (ret < 0)
 			return ret;
 		buf += now;
@@ -157,9 +157,7 @@ static int nand_bb_erase(struct cdev *cdev, size_t count, unsigned long offset)
 		return -EINVAL;
 	}
 
-	lseek(bb->fd, 0, SEEK_SET);
-
-	return erase(bb->fd, bb->raw_size, 0);
+	return cdev_erase(bb->cdev_parent, bb->raw_size, 0);
 }
 #endif
 
@@ -198,7 +196,7 @@ static int nand_bb_calc_size(struct nand_bb *bb)
 	int ret;
 
 	while (pos < bb->raw_size) {
-		ret = ioctl(bb->fd, MEMGETBADBLOCK, (void *)pos);
+		ret = cdev_ioctl(bb->cdev_parent, MEMGETBADBLOCK, (void *)pos);
 		if (ret < 0)
 			return ret;
 		if (!ret)
@@ -230,34 +228,25 @@ int dev_add_bb_dev(char *path, const char *name)
 {
 	struct nand_bb *bb;
 	int ret = -ENOMEM;
-	struct stat s;
 
 	bb = xzalloc(sizeof(*bb));
-	bb->devname = asprintf("/dev/%s", basename(path));
-	if (!bb->devname)
-		goto out1;
-
-	if (name)
-		bb->cdev.name = strdup(name);
-	else
-		bb->cdev.name = asprintf("%s.bb", basename(path));
 
-	if (!bb->cdev.name)
-		goto out2;
+	bb->cdev_parent = cdev_open(path, O_RDWR);
+	if (!bb->cdev_parent)
+		goto out1;
 
-	ret = stat(bb->devname, &s);
-	if (ret)
-		goto out3;
+	if (name) {
+		strcpy(bb->cdevname, name);
+	} else {
+		strcpy(bb->cdevname, path);
+		strcat(bb->cdevname, ".bb");
+	}
 
-	bb->raw_size = s.st_size;
+	bb->cdev.name = bb->cdevname;
 
-	bb->fd = open(bb->devname, O_RDWR);
-	if (bb->fd < 0) {
-		ret = -ENODEV;
-		goto out3;
-	}
+	bb->raw_size = bb->cdev_parent->size;
 
-	ret = ioctl(bb->fd, MEMGETINFO, &bb->info);
+	ret = cdev_ioctl(bb->cdev_parent, MEMGETINFO, &bb->info);
 	if (ret)
 		goto out4;
 
@@ -265,16 +254,14 @@ int dev_add_bb_dev(char *path, const char *name)
 	bb->cdev.ops = &nand_bb_ops;
 	bb->cdev.priv = bb;
 
-	devfs_create(&bb->cdev);
+	ret = devfs_create(&bb->cdev);
+	if (ret)
+		goto out4;
 
 	return 0;
 
 out4:
-	close(bb->fd);
-out3:
-	free(bb->cdev.name);
-out2:
-	free(bb->devname);
+	cdev_close(bb->cdev_parent);
 out1:
 	free(bb);
 	return ret;
-- 
1.7.2.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] nand bb: add proper bb remove function
  2011-04-11 14:11 nand patches Sascha Hauer
                   ` (2 preceding siblings ...)
  2011-04-11 14:11 ` [PATCH 3/4] nand bb: switch to cdev operations Sascha Hauer
@ 2011-04-11 14:11 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2011-04-11 14:11 UTC (permalink / raw)
  To: barebox

The old way happily removed cdev entries which were no bb dev
at all. Fix this by checking if the given device actually is
a bb device.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/nand.c            |   13 +------------
 drivers/mtd/nand/nand-bb.c |   22 ++++++++++++++++++++++
 include/nand.h             |    5 +++++
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/commands/nand.c b/commands/nand.c
index ed55625..88f242d 100644
--- a/commands/nand.c
+++ b/commands/nand.c
@@ -39,7 +39,6 @@
 static int do_nand(struct command *cmdtp, int argc, char *argv[])
 {
 	int opt;
-	struct nand_bb *bb;
 	int command = 0, badblock = 0;
 
 	while((opt = getopt(argc, argv, "adb:")) > 0) {
@@ -72,17 +71,7 @@ static int do_nand(struct command *cmdtp, int argc, char *argv[])
 
 	if (command & NAND_DEL) {
 		while (optind < argc) {
-			struct cdev *cdev;
-
-			cdev = cdev_by_name(basename(argv[optind]));
-			if (!cdev) {
-				printf("no such device: %s\n", argv[optind]);
-				return 1;
-			}
-			bb = cdev->priv;
-			close(bb->fd);
-			devfs_remove(cdev);
-			free(bb);
+			dev_remove_bb_dev(basename(argv[optind]));
 			optind++;
 		}
 	}
diff --git a/drivers/mtd/nand/nand-bb.c b/drivers/mtd/nand/nand-bb.c
index d71a2b0..dbfb8e3 100644
--- a/drivers/mtd/nand/nand-bb.c
+++ b/drivers/mtd/nand/nand-bb.c
@@ -31,6 +31,7 @@
 #include <linux/mtd/mtd-abi.h>
 #include <fcntl.h>
 #include <libgen.h>
+#include <linux/list.h>
 
 struct nand_bb {
 	char cdevname[MAX_DRIVER_NAME];
@@ -47,6 +48,8 @@ struct nand_bb {
 	void *writebuf;
 
 	struct cdev cdev;
+
+	struct list_head list;
 };
 
 static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
@@ -218,6 +221,8 @@ static struct file_operations nand_bb_ops = {
 #endif
 };
 
+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
@@ -258,6 +263,8 @@ int dev_add_bb_dev(char *path, const char *name)
 	if (ret)
 		goto out4;
 
+	list_add_tail(&bb->list, &bb_list);
+
 	return 0;
 
 out4:
@@ -267,3 +274,18 @@ out1:
 	return ret;
 }
 
+int dev_remove_bb_dev(const char *name)
+{
+	struct nand_bb *bb;
+
+	list_for_each_entry(bb, &bb_list, list) {
+		if (!strcmp(bb->cdev.name, name)) {
+			devfs_remove(&bb->cdev);
+			cdev_close(bb->cdev_parent);
+			free(bb);
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
diff --git a/include/nand.h b/include/nand.h
index 05358d0..b1762df 100644
--- a/include/nand.h
+++ b/include/nand.h
@@ -6,10 +6,15 @@ struct nand_bb;
 
 #ifdef CONFIG_NAND
 int dev_add_bb_dev(char *filename, const char *name);
+int dev_remove_bb_dev(const char *name);
 #else
 static inline int dev_add_bb_dev(char *filename, const char *name) {
 	return 0;
 }
+static inline int dev_remove_bb_dev(const char *name)
+{
+	return 0;
+}
 #endif
 
 #endif /* __NAND_H__ */
-- 
1.7.2.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-04-11 14:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-11 14:11 nand patches Sascha Hauer
2011-04-11 14:11 ` [PATCH 1/4] nand: nand_block_markbad is only used with nand write support Sascha Hauer
2011-04-11 14:11 ` [PATCH 2/4] nand: move bb handling code to drivers/mtd/nand Sascha Hauer
2011-04-11 14:11 ` [PATCH 3/4] nand bb: switch to cdev operations Sascha Hauer
2011-04-11 14:11 ` [PATCH 4/4] nand bb: add proper bb remove function Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox