9
0
Fork 0

mtd: nand: bb: use mtd api directly

The devfs layer just adds an addition indirection between mtd
and the bb devices with no purpose. Drop it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2014-04-09 15:49:32 +02:00
parent ebb8a4b594
commit b32cd8df87
1 changed files with 51 additions and 73 deletions

View File

@ -25,22 +25,18 @@
#include <init.h>
#include <ioctl.h>
#include <nand.h>
#include <linux/mtd/mtd-abi.h>
#include <linux/mtd/mtd.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/list.h>
struct nand_bb {
char cdevname[MAX_DRIVER_NAME];
struct cdev *cdev_parent;
char *name;
int open;
int needs_write;
struct mtd_info_user info;
struct mtd_info *mtd;
loff_t raw_size;
loff_t size;
loff_t offset;
unsigned long flags;
void *writebuf;
@ -54,31 +50,28 @@ static ssize_t nand_bb_read(struct cdev *cdev, void *buf, size_t count,
loff_t offset, ulong flags)
{
struct nand_bb *bb = cdev->priv;
struct cdev *parent = bb->cdev_parent;
size_t retlen;
int ret, bytes = 0, now;
debug("%s 0x%08llx %d\n", __func__, offset, count);
while(count) {
ret = cdev_ioctl(parent, MEMGETBADBLOCK, &bb->offset);
if (ret < 0)
return ret;
if (ret) {
while (count) {
if (mtd_block_isbad(bb->mtd, offset)) {
printf("skipping bad block at 0x%08llx\n", bb->offset);
bb->offset += bb->info.erasesize;
bb->offset += bb->mtd->erasesize;
continue;
}
now = min(count, (size_t)(bb->info.erasesize -
((size_t)bb->offset % bb->info.erasesize)));
ret = cdev_read(parent, buf, now, bb->offset, 0);
now = min(count, (size_t)(bb->mtd->erasesize -
((size_t)bb->offset % bb->mtd->erasesize)));
ret = mtd_read(bb->mtd, bb->offset, now, &retlen, buf);
if (ret < 0)
return ret;
buf += now;
count -= now;
bb->offset += now;
bytes += now;
buf += retlen;
count -= retlen;
bb->offset += retlen;
bytes += retlen;
};
return bytes;
@ -91,29 +84,25 @@ 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;
size_t retlen;
void *buf = bb->writebuf;
loff_t cur_ofs = bb->offset & ~(BB_WRITEBUF_SIZE - 1);
while (count) {
ret = cdev_ioctl(parent, MEMGETBADBLOCK, &cur_ofs);
if (ret < 0)
return ret;
if (ret) {
if (mtd_block_isbad(bb->mtd, cur_ofs)) {
debug("skipping bad block at 0x%08llx\n", cur_ofs);
bb->offset += bb->info.erasesize;
cur_ofs += bb->info.erasesize;
bb->offset += bb->mtd->erasesize;
cur_ofs += bb->mtd->erasesize;
continue;
}
now = min(count, (size_t)(bb->info.erasesize));
ret = cdev_write(parent, buf, now, cur_ofs, 0);
now = min(count, (size_t)(bb->mtd->erasesize));
ret = mtd_write(bb->mtd, cur_ofs, now, &retlen, buf);
if (ret < 0)
return ret;
buf += now;
count -= now;
cur_ofs += now;
buf += retlen;
count -= retlen;
cur_ofs += retlen;
};
return 0;
@ -152,13 +141,17 @@ static ssize_t nand_bb_write(struct cdev *cdev, const void *buf, size_t count,
static int nand_bb_erase(struct cdev *cdev, size_t count, loff_t offset)
{
struct nand_bb *bb = cdev->priv;
struct erase_info erase = {};
if (offset != 0) {
printf("can only erase from beginning of device\n");
return -EINVAL;
}
return cdev_erase(bb->cdev_parent, bb->raw_size, 0);
erase.addr = 0;
erase.len = bb->mtd->size;
return mtd_erase(bb->mtd, &erase);
}
#endif
@ -195,16 +188,12 @@ static int nand_bb_close(struct cdev *cdev)
static int nand_bb_calc_size(struct nand_bb *bb)
{
loff_t pos = 0;
int ret;
while (pos < bb->raw_size) {
ret = cdev_ioctl(bb->cdev_parent, MEMGETBADBLOCK, &pos);
if (ret < 0)
return ret;
if (!ret)
bb->cdev.size += bb->info.erasesize;
while (pos < bb->mtd->size) {
if (!mtd_block_isbad(bb->mtd, pos))
bb->cdev.size += bb->mtd->erasesize;
pos += bb->info.erasesize;
pos += bb->mtd->erasesize;
}
return 0;
@ -215,22 +204,18 @@ static loff_t nand_bb_lseek(struct cdev *cdev, loff_t __offset)
struct nand_bb *bb = cdev->priv;
loff_t raw_pos = 0;
uint32_t offset = __offset;
int ret;
/* lseek only in readonly mode */
if (bb->flags & O_ACCMODE)
return -ENOSYS;
while (raw_pos < bb->raw_size) {
off_t now = min(offset, bb->info.erasesize);
while (raw_pos < bb->mtd->size) {
off_t now = min(offset, bb->mtd->erasesize);
ret = cdev_ioctl(bb->cdev_parent, MEMGETBADBLOCK, &raw_pos);
if (ret < 0)
return ret;
if (!ret) {
if (mtd_block_isbad(bb->mtd, raw_pos)) {
raw_pos += bb->mtd->erasesize;
} else {
offset -= now;
raw_pos += now;
} else {
raw_pos += bb->info.erasesize;
}
if (!offset) {
@ -264,28 +249,23 @@ static LIST_HEAD(bb_list);
int dev_add_bb_dev(const char *path, 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;
bb = xzalloc(sizeof(*bb));
bb->mtd = parent->mtd;
bb->cdev_parent = cdev_open(path, O_RDWR);
if (!bb->cdev_parent)
goto out1;
if (name) {
strcpy(bb->cdevname, name);
} else {
strcpy(bb->cdevname, path);
strcat(bb->cdevname, ".bb");
}
bb->cdev.name = bb->cdevname;
bb->raw_size = bb->cdev_parent->size;
ret = cdev_ioctl(bb->cdev_parent, MEMGETINFO, &bb->info);
if (ret)
goto out4;
if (name)
bb->cdev.name = xstrdup(name);
else
bb->cdev.name = asprintf("%s.bb", path);;
nand_bb_calc_size(bb);
bb->cdev.ops = &nand_bb_ops;
@ -300,8 +280,6 @@ int dev_add_bb_dev(const char *path, const char *name)
return 0;
out4:
cdev_close(bb->cdev_parent);
out1:
free(bb);
return ret;
}
@ -313,8 +291,8 @@ int dev_remove_bb_dev(const char *name)
list_for_each_entry(bb, &bb_list, list) {
if (!strcmp(bb->cdev.name, name)) {
devfs_remove(&bb->cdev);
cdev_close(bb->cdev_parent);
list_del_init(&bb->list);
free(bb->name);
free(bb);
return 0;
}