From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 3/4] nand bb: switch to cdev operations
Date: Mon, 11 Apr 2011 16:11:50 +0200 [thread overview]
Message-ID: <1302531111-29244-4-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1302531111-29244-1-git-send-email-s.hauer@pengutronix.de>
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
next prev parent reply other threads:[~2011-04-11 14:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2011-04-11 14:11 ` [PATCH 4/4] nand bb: add proper bb remove function Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1302531111-29244-4-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox