2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2010-08-08 19:58:20 +00:00
|
|
|
* Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/fs.h>
|
2007-08-10 20:01:06 +00:00
|
|
|
#include <linux/mm.h>
|
2006-10-11 11:52:47 +00:00
|
|
|
#include <linux/err.h>
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <linux/init.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/sched.h>
|
2010-06-02 12:28:52 +00:00
|
|
|
#include <linux/mutex.h>
|
2009-02-12 10:40:00 +00:00
|
|
|
#include <linux/backing-dev.h>
|
2009-04-09 05:53:13 +00:00
|
|
|
#include <linux/compat.h>
|
2010-05-17 13:55:47 +00:00
|
|
|
#include <linux/mount.h>
|
2010-09-17 10:31:42 +00:00
|
|
|
#include <linux/blkpg.h>
|
2012-03-23 22:01:50 +00:00
|
|
|
#include <linux/magic.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
2010-09-17 10:31:42 +00:00
|
|
|
#include <linux/mtd/partitions.h>
|
2010-06-15 07:30:15 +00:00
|
|
|
#include <linux/mtd/map.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <asm/uaccess.h>
|
2005-06-30 00:23:27 +00:00
|
|
|
|
2010-06-02 12:28:52 +00:00
|
|
|
static DEFINE_MUTEX(mtd_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-02-08 19:12:53 +00:00
|
|
|
/*
|
2006-05-29 22:37:34 +00:00
|
|
|
* Data structure to hold the pointer to the mtd device as well
|
2011-07-20 16:53:42 +00:00
|
|
|
* as mode information of various use cases.
|
2005-02-08 19:12:53 +00:00
|
|
|
*/
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info {
|
|
|
|
struct mtd_info *mtd;
|
2010-05-17 13:55:47 +00:00
|
|
|
struct inode *ino;
|
2006-05-29 22:37:34 +00:00
|
|
|
enum mtd_file_modes mode;
|
|
|
|
};
|
2005-02-08 17:45:55 +00:00
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
switch (orig) {
|
2006-09-17 01:09:29 +00:00
|
|
|
case SEEK_SET:
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2006-09-17 01:09:29 +00:00
|
|
|
case SEEK_CUR:
|
2005-08-04 01:05:51 +00:00
|
|
|
offset += file->f_pos;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
2006-09-17 01:09:29 +00:00
|
|
|
case SEEK_END:
|
2005-08-04 01:05:51 +00:00
|
|
|
offset += mtd->size;
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
[MTD] CORE mtdchar.c: fix off-by-one error in lseek()
Allow lseek(mtdchar_fd, 0, SEEK_END) to succeed, which currently fails
with EINVAL.
lseek(fd, 0, SEEK_END) should result into the same fileposition as
lseek(fd, 0, SEEK_SET) + read(fd, buf, length(fd))
Furthermore, lseek(fd, 0, SEEK_CUR) should return the current file position,
which in case of an encountered EOF should not result in EINVAL
Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
2006-06-23 22:03:36 +00:00
|
|
|
if (offset >= 0 && offset <= mtd->size)
|
2005-08-04 01:05:51 +00:00
|
|
|
return file->f_pos = offset;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-08-04 01:05:51 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 17:47:52 +00:00
|
|
|
static int count;
|
|
|
|
static struct vfsmount *mnt;
|
|
|
|
static struct file_system_type mtd_inodefs_type;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_open(struct inode *inode, struct file *file)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int minor = iminor(inode);
|
|
|
|
int devnum = minor >> 1;
|
2008-05-15 16:10:37 +00:00
|
|
|
int ret = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct mtd_info *mtd;
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi;
|
2010-05-17 13:55:47 +00:00
|
|
|
struct inode *mtd_ino;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("MTD_open\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* You can't open the RO devices RW */
|
2008-09-02 19:28:45 +00:00
|
|
|
if ((file->f_mode & FMODE_WRITE) && (minor & 1))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EACCES;
|
|
|
|
|
2012-03-23 17:47:52 +00:00
|
|
|
ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_lock(&mtd_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
mtd = get_mtd_device(NULL, devnum);
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2008-05-15 16:10:37 +00:00
|
|
|
if (IS_ERR(mtd)) {
|
|
|
|
ret = PTR_ERR(mtd);
|
|
|
|
goto out;
|
|
|
|
}
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2009-02-12 10:40:00 +00:00
|
|
|
if (mtd->type == MTD_ABSENT) {
|
2008-05-15 16:10:37 +00:00
|
|
|
ret = -ENODEV;
|
2012-04-09 05:36:28 +00:00
|
|
|
goto out1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 17:47:52 +00:00
|
|
|
mtd_ino = iget_locked(mnt->mnt_sb, devnum);
|
2010-05-17 13:55:47 +00:00
|
|
|
if (!mtd_ino) {
|
|
|
|
ret = -ENOMEM;
|
2012-04-09 05:36:28 +00:00
|
|
|
goto out1;
|
2010-05-17 13:55:47 +00:00
|
|
|
}
|
|
|
|
if (mtd_ino->i_state & I_NEW) {
|
|
|
|
mtd_ino->i_private = mtd;
|
|
|
|
mtd_ino->i_mode = S_IFCHR;
|
|
|
|
mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
|
|
|
|
unlock_new_inode(mtd_ino);
|
|
|
|
}
|
|
|
|
file->f_mapping = mtd_ino->i_mapping;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* You can't open it RW if it's not a writeable device */
|
2008-09-02 19:28:45 +00:00
|
|
|
if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
|
2008-05-15 16:10:37 +00:00
|
|
|
ret = -EACCES;
|
2012-04-09 05:36:28 +00:00
|
|
|
goto out2;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
|
|
|
|
if (!mfi) {
|
2008-05-15 16:10:37 +00:00
|
|
|
ret = -ENOMEM;
|
2012-04-09 05:36:28 +00:00
|
|
|
goto out2;
|
2006-05-29 22:37:34 +00:00
|
|
|
}
|
2010-05-17 13:55:47 +00:00
|
|
|
mfi->ino = mtd_ino;
|
2006-05-29 22:37:34 +00:00
|
|
|
mfi->mtd = mtd;
|
|
|
|
file->private_data = mfi;
|
2012-04-09 05:36:28 +00:00
|
|
|
mutex_unlock(&mtd_mutex);
|
|
|
|
return 0;
|
2006-05-29 22:37:34 +00:00
|
|
|
|
2012-04-09 05:36:28 +00:00
|
|
|
out2:
|
|
|
|
iput(mtd_ino);
|
|
|
|
out1:
|
|
|
|
put_mtd_device(mtd);
|
2008-05-15 16:10:37 +00:00
|
|
|
out:
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_unlock(&mtd_mutex);
|
2012-03-23 17:47:52 +00:00
|
|
|
simple_release_fs(&mnt, &count);
|
2008-05-15 16:10:37 +00:00
|
|
|
return ret;
|
2011-12-23 15:27:46 +00:00
|
|
|
} /* mtdchar_open */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*====================================================================*/
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_close(struct inode *inode, struct file *file)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("MTD_close\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-06-26 22:56:40 +00:00
|
|
|
/* Only sync if opened RW */
|
2011-12-30 14:35:35 +00:00
|
|
|
if ((file->f_mode & FMODE_WRITE))
|
2011-12-23 17:03:12 +00:00
|
|
|
mtd_sync(mtd);
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2010-05-17 13:55:47 +00:00
|
|
|
iput(mfi->ino);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
put_mtd_device(mtd);
|
2006-05-29 22:37:34 +00:00
|
|
|
file->private_data = NULL;
|
|
|
|
kfree(mfi);
|
2012-03-23 17:47:52 +00:00
|
|
|
simple_release_fs(&mnt, &count);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-12-23 15:27:46 +00:00
|
|
|
} /* mtdchar_close */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-04-08 15:51:33 +00:00
|
|
|
/* Back in June 2001, dwmw2 wrote:
|
|
|
|
*
|
|
|
|
* FIXME: This _really_ needs to die. In 2.5, we should lock the
|
|
|
|
* userspace buffer down and use it directly with readv/writev.
|
|
|
|
*
|
|
|
|
* The implementation below, using mtd_kmalloc_up_to, mitigates
|
|
|
|
* allocation failures when the system is under low-memory situations
|
|
|
|
* or if memory is highly fragmented at the cost of reducing the
|
|
|
|
* performance of the requested transfer due to a smaller buffer size.
|
|
|
|
*
|
|
|
|
* A more complex but more memory-efficient implementation based on
|
|
|
|
* get_user_pages and iovecs to cover extents of those pages is a
|
|
|
|
* longer-term goal, as intimated by dwmw2 above. However, for the
|
|
|
|
* write case, this requires yet more complex head and tail transfer
|
|
|
|
* handling when those head and tail offsets and sizes are such that
|
|
|
|
* alignment requirements are not met in the NAND subdriver.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
|
|
|
|
loff_t *ppos)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
mtd: remove extra retlen assignment
MTD functions always assign the 'retlen' argument to 0 at the very
beginning - the callers do not have to do this.
I used the following semantic patch to find these places:
@@
identifier retlen;
expression a, b, c, d, e;
constant C;
type T;
@@
(
- retlen = C;
|
T
-retlen = C
+ retlen
;
)
... when != retlen
when exists
(
mtd_read(a, b, c, &retlen, d)
|
mtd_write(a, b, c, &retlen, d)
|
mtd_panic_write(a, b, c, &retlen, d)
|
mtd_point(a, b, c, &retlen, d, e)
|
mtd_read_fact_prot_reg(a, b, c, &retlen, d)
|
mtd_write_user_prot_reg(a, b, c, &retlen, d)
|
mtd_read_user_prot_reg(a, b, c, &retlen, d)
|
mtd_writev(a, b, c, d, &retlen)
)
I ran it twice, because there were cases of double zero assigments
in mtd tests. Then I went through the patch to verify that spatch
did not find any false positives.
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
2011-12-29 13:16:28 +00:00
|
|
|
size_t retlen;
|
2005-04-16 22:20:36 +00:00
|
|
|
size_t total_retlen=0;
|
|
|
|
int ret=0;
|
|
|
|
int len;
|
2011-04-08 15:51:33 +00:00
|
|
|
size_t size = count;
|
2005-04-16 22:20:36 +00:00
|
|
|
char *kbuf;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("MTD_read\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (*ppos + count > mtd->size)
|
|
|
|
count = mtd->size - *ppos;
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2011-04-08 15:51:33 +00:00
|
|
|
kbuf = mtd_kmalloc_up_to(mtd, &size);
|
2006-04-17 16:38:15 +00:00
|
|
|
if (!kbuf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
while (count) {
|
2011-04-08 15:51:33 +00:00
|
|
|
len = min_t(size_t, count, size);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
switch (mfi->mode) {
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_FACTORY:
|
2011-12-23 16:40:06 +00:00
|
|
|
ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
|
|
|
|
&retlen, kbuf);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_USER:
|
2011-12-23 16:47:59 +00:00
|
|
|
ret = mtd_read_user_prot_reg(mtd, *ppos, len,
|
|
|
|
&retlen, kbuf);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_RAW:
|
2006-05-29 22:37:34 +00:00
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
|
2011-08-31 01:45:40 +00:00
|
|
|
ops.mode = MTD_OPS_RAW;
|
2006-05-29 22:37:34 +00:00
|
|
|
ops.datbuf = kbuf;
|
|
|
|
ops.oobbuf = NULL;
|
|
|
|
ops.len = len;
|
|
|
|
|
2011-12-23 16:27:05 +00:00
|
|
|
ret = mtd_read_oob(mtd, *ppos, &ops);
|
2006-05-29 22:37:34 +00:00
|
|
|
retlen = ops.retlen;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-08 17:45:55 +00:00
|
|
|
default:
|
2011-12-23 15:30:16 +00:00
|
|
|
ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
|
2005-02-08 17:45:55 +00:00
|
|
|
}
|
2011-06-23 21:12:08 +00:00
|
|
|
/* Nand returns -EBADMSG on ECC errors, but it returns
|
2005-04-16 22:20:36 +00:00
|
|
|
* the data. For our userspace tools it is important
|
2011-06-23 21:12:08 +00:00
|
|
|
* to dump areas with ECC errors!
|
2006-05-29 12:56:39 +00:00
|
|
|
* For kernel internal usage it also might return -EUCLEAN
|
2011-03-31 01:57:33 +00:00
|
|
|
* to signal the caller that a bitflip has occurred and has
|
2006-05-29 12:56:39 +00:00
|
|
|
* been corrected by the ECC algorithm.
|
2005-04-16 22:20:36 +00:00
|
|
|
* Userspace software which accesses NAND this way
|
|
|
|
* must be aware of the fact that it deals with NAND
|
|
|
|
*/
|
2011-09-21 01:34:25 +00:00
|
|
|
if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
*ppos += retlen;
|
|
|
|
if (copy_to_user(buf, kbuf, retlen)) {
|
2006-05-28 09:01:53 +00:00
|
|
|
kfree(kbuf);
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
total_retlen += retlen;
|
|
|
|
|
|
|
|
count -= retlen;
|
|
|
|
buf += retlen;
|
2005-02-08 17:45:55 +00:00
|
|
|
if (retlen == 0)
|
|
|
|
count = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
kfree(kbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-04-17 16:38:15 +00:00
|
|
|
kfree(kbuf);
|
2005-04-16 22:20:36 +00:00
|
|
|
return total_retlen;
|
2011-12-23 15:27:46 +00:00
|
|
|
} /* mtdchar_read */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
|
|
|
|
loff_t *ppos)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2011-04-08 15:51:33 +00:00
|
|
|
size_t size = count;
|
2005-04-16 22:20:36 +00:00
|
|
|
char *kbuf;
|
|
|
|
size_t retlen;
|
|
|
|
size_t total_retlen=0;
|
|
|
|
int ret=0;
|
|
|
|
int len;
|
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("MTD_write\n");
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (*ppos == mtd->size)
|
|
|
|
return -ENOSPC;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (*ppos + count > mtd->size)
|
|
|
|
count = mtd->size - *ppos;
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
2011-04-08 15:51:33 +00:00
|
|
|
kbuf = mtd_kmalloc_up_to(mtd, &size);
|
2006-04-17 16:38:15 +00:00
|
|
|
if (!kbuf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
while (count) {
|
2011-04-08 15:51:33 +00:00
|
|
|
len = min_t(size_t, count, size);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (copy_from_user(kbuf, buf, len)) {
|
|
|
|
kfree(kbuf);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
switch (mfi->mode) {
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_FACTORY:
|
2005-02-08 17:45:55 +00:00
|
|
|
ret = -EROFS;
|
|
|
|
break;
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_USER:
|
2011-12-23 16:50:04 +00:00
|
|
|
ret = mtd_write_user_prot_reg(mtd, *ppos, len,
|
|
|
|
&retlen, kbuf);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
2006-05-29 22:37:34 +00:00
|
|
|
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_RAW:
|
2006-05-29 22:37:34 +00:00
|
|
|
{
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
|
2011-08-31 01:45:40 +00:00
|
|
|
ops.mode = MTD_OPS_RAW;
|
2006-05-29 22:37:34 +00:00
|
|
|
ops.datbuf = kbuf;
|
|
|
|
ops.oobbuf = NULL;
|
2011-06-06 13:50:58 +00:00
|
|
|
ops.ooboffs = 0;
|
2006-05-29 22:37:34 +00:00
|
|
|
ops.len = len;
|
|
|
|
|
2011-12-23 16:29:55 +00:00
|
|
|
ret = mtd_write_oob(mtd, *ppos, &ops);
|
2006-05-29 22:37:34 +00:00
|
|
|
retlen = ops.retlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-02-08 17:45:55 +00:00
|
|
|
default:
|
2011-12-23 15:35:41 +00:00
|
|
|
ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
|
2005-02-08 17:45:55 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!ret) {
|
|
|
|
*ppos += retlen;
|
|
|
|
total_retlen += retlen;
|
|
|
|
count -= retlen;
|
|
|
|
buf += retlen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kfree(kbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-17 16:38:15 +00:00
|
|
|
kfree(kbuf);
|
2005-04-16 22:20:36 +00:00
|
|
|
return total_retlen;
|
2011-12-23 15:27:46 +00:00
|
|
|
} /* mtdchar_write */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*======================================================================
|
|
|
|
|
|
|
|
IOCTL calls for getting device parameters.
|
|
|
|
|
|
|
|
======================================================================*/
|
|
|
|
static void mtdchar_erase_callback (struct erase_info *instr)
|
|
|
|
{
|
|
|
|
wake_up((wait_queue_head_t *)instr->priv);
|
|
|
|
}
|
|
|
|
|
2008-07-30 19:35:05 +00:00
|
|
|
#ifdef CONFIG_HAVE_MTD_OTP
|
2006-05-29 22:37:34 +00:00
|
|
|
static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2011-12-29 08:06:32 +00:00
|
|
|
size_t retlen;
|
2006-05-29 22:37:34 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-12-29 08:06:32 +00:00
|
|
|
/*
|
|
|
|
* Make a fake call to mtd_read_fact_prot_reg() to check if OTP
|
|
|
|
* operations are supported.
|
|
|
|
*/
|
2012-03-30 10:51:02 +00:00
|
|
|
if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP)
|
2011-12-29 08:06:32 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
switch (mode) {
|
|
|
|
case MTD_OTP_FACTORY:
|
2011-12-29 08:06:32 +00:00
|
|
|
mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
|
2006-05-29 22:37:34 +00:00
|
|
|
break;
|
|
|
|
case MTD_OTP_USER:
|
2011-12-29 08:06:32 +00:00
|
|
|
mfi->mode = MTD_FILE_MODE_OTP_USER;
|
2006-05-29 22:37:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
case MTD_OTP_OFF:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define otp_select_filemode(f,m) -EOPNOTSUPP
|
|
|
|
#endif
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
|
2009-04-09 05:53:13 +00:00
|
|
|
uint64_t start, uint32_t length, void __user *ptr,
|
|
|
|
uint32_t __user *retp)
|
|
|
|
{
|
2011-08-31 01:45:37 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
2009-04-09 05:53:13 +00:00
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
uint32_t retlen;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!(file->f_mode & FMODE_WRITE))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (length > 4096)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-01-30 12:58:32 +00:00
|
|
|
if (!mtd->_write_oob)
|
2009-04-09 05:53:13 +00:00
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
else
|
2010-01-29 09:35:04 +00:00
|
|
|
ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ops.ooblen = length;
|
mtd: do not assume oobsize is power of 2
Previous generations of MTDs all used OOB sizes that were powers of 2,
(e.g., 64, 128). However, newer generations of flash, especially NAND,
use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448).
This means we cannot use masks like "mtd->oobsize - 1" to assume that we
will get a proper bitmask for OOB operations.
These masks are really only intended to hide the "page" portion of the
offset, leaving any OOB offset intact, so a masking with the writesize
(which *is* always a power of 2) is valid and makes more sense.
This has been tested for read/write of NAND devices (nanddump/nandwrite)
using nandsim and actual NAND flash.
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-08-24 00:17:32 +00:00
|
|
|
ops.ooboffs = start & (mtd->writesize - 1);
|
2009-04-09 05:53:13 +00:00
|
|
|
ops.datbuf = NULL;
|
2011-08-31 01:45:41 +00:00
|
|
|
ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
|
2011-08-31 01:45:40 +00:00
|
|
|
MTD_OPS_PLACE_OOB;
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-05-22 08:22:49 +00:00
|
|
|
ops.oobbuf = memdup_user(ptr, length);
|
|
|
|
if (IS_ERR(ops.oobbuf))
|
|
|
|
return PTR_ERR(ops.oobbuf);
|
2009-04-09 05:53:13 +00:00
|
|
|
|
mtd: do not assume oobsize is power of 2
Previous generations of MTDs all used OOB sizes that were powers of 2,
(e.g., 64, 128). However, newer generations of flash, especially NAND,
use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448).
This means we cannot use masks like "mtd->oobsize - 1" to assume that we
will get a proper bitmask for OOB operations.
These masks are really only intended to hide the "page" portion of the
offset, leaving any OOB offset intact, so a masking with the writesize
(which *is* always a power of 2) is valid and makes more sense.
This has been tested for read/write of NAND devices (nanddump/nandwrite)
using nandsim and actual NAND flash.
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-08-24 00:17:32 +00:00
|
|
|
start &= ~((uint64_t)mtd->writesize - 1);
|
2011-12-23 16:29:55 +00:00
|
|
|
ret = mtd_write_oob(mtd, start, &ops);
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
if (ops.oobretlen > 0xFFFFFFFFU)
|
|
|
|
ret = -EOVERFLOW;
|
|
|
|
retlen = ops.oobretlen;
|
|
|
|
if (copy_to_user(retp, &retlen, sizeof(length)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
|
|
|
|
kfree(ops.oobbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
|
2011-08-31 01:45:38 +00:00
|
|
|
uint64_t start, uint32_t length, void __user *ptr,
|
|
|
|
uint32_t __user *retp)
|
2009-04-09 05:53:13 +00:00
|
|
|
{
|
2011-08-31 01:45:38 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
2009-04-09 05:53:13 +00:00
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (length > 4096)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-12-28 15:50:34 +00:00
|
|
|
if (!access_ok(VERIFY_WRITE, ptr, length))
|
|
|
|
return -EFAULT;
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
ops.ooblen = length;
|
mtd: do not assume oobsize is power of 2
Previous generations of MTDs all used OOB sizes that were powers of 2,
(e.g., 64, 128). However, newer generations of flash, especially NAND,
use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448).
This means we cannot use masks like "mtd->oobsize - 1" to assume that we
will get a proper bitmask for OOB operations.
These masks are really only intended to hide the "page" portion of the
offset, leaving any OOB offset intact, so a masking with the writesize
(which *is* always a power of 2) is valid and makes more sense.
This has been tested for read/write of NAND devices (nanddump/nandwrite)
using nandsim and actual NAND flash.
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-08-24 00:17:32 +00:00
|
|
|
ops.ooboffs = start & (mtd->writesize - 1);
|
2009-04-09 05:53:13 +00:00
|
|
|
ops.datbuf = NULL;
|
2011-08-31 01:45:41 +00:00
|
|
|
ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
|
2011-08-31 01:45:40 +00:00
|
|
|
MTD_OPS_PLACE_OOB;
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ops.oobbuf = kmalloc(length, GFP_KERNEL);
|
|
|
|
if (!ops.oobbuf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
mtd: do not assume oobsize is power of 2
Previous generations of MTDs all used OOB sizes that were powers of 2,
(e.g., 64, 128). However, newer generations of flash, especially NAND,
use irregular OOB sizes that are not powers of 2 (e.g., 218, 224, 448).
This means we cannot use masks like "mtd->oobsize - 1" to assume that we
will get a proper bitmask for OOB operations.
These masks are really only intended to hide the "page" portion of the
offset, leaving any OOB offset intact, so a masking with the writesize
(which *is* always a power of 2) is valid and makes more sense.
This has been tested for read/write of NAND devices (nanddump/nandwrite)
using nandsim and actual NAND flash.
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
2011-08-24 00:17:32 +00:00
|
|
|
start &= ~((uint64_t)mtd->writesize - 1);
|
2011-12-23 16:27:05 +00:00
|
|
|
ret = mtd_read_oob(mtd, start, &ops);
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
if (put_user(ops.oobretlen, retp))
|
|
|
|
ret = -EFAULT;
|
|
|
|
else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
|
|
|
|
ops.oobretlen))
|
|
|
|
ret = -EFAULT;
|
|
|
|
|
|
|
|
kfree(ops.oobbuf);
|
2011-06-23 23:45:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NAND returns -EBADMSG on ECC errors, but it returns the OOB
|
|
|
|
* data. For our userspace tools it is important to dump areas
|
|
|
|
* with ECC errors!
|
|
|
|
* For kernel internal usage it also might return -EUCLEAN
|
|
|
|
* to signal the caller that a bitflip has occured and has
|
|
|
|
* been corrected by the ECC algorithm.
|
|
|
|
*
|
2011-06-28 23:29:00 +00:00
|
|
|
* Note: currently the standard NAND function, nand_read_oob_std,
|
|
|
|
* does not calculate ECC for the OOB area, so do not rely on
|
|
|
|
* this behavior unless you have replaced it with your own.
|
2011-06-23 23:45:24 +00:00
|
|
|
*/
|
2011-09-21 01:34:25 +00:00
|
|
|
if (mtd_is_bitflip_or_eccerr(ret))
|
2011-06-23 23:45:24 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-04-09 05:53:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:12:00 +00:00
|
|
|
/*
|
|
|
|
* Copies (and truncates, if necessary) data from the larger struct,
|
|
|
|
* nand_ecclayout, to the smaller, deprecated layout struct,
|
2011-07-20 16:53:42 +00:00
|
|
|
* nand_ecclayout_user. This is necessary only to support the deprecated
|
2010-08-25 01:12:00 +00:00
|
|
|
* API ioctl ECCGETLAYOUT while allowing all new functionality to use
|
|
|
|
* nand_ecclayout flexibly (i.e. the struct may change size in new
|
|
|
|
* releases without requiring major rewrites).
|
|
|
|
*/
|
|
|
|
static int shrink_ecclayout(const struct nand_ecclayout *from,
|
|
|
|
struct nand_ecclayout_user *to)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!from || !to)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
memset(to, 0, sizeof(*to));
|
|
|
|
|
2010-09-20 06:57:12 +00:00
|
|
|
to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
|
2010-08-25 01:12:00 +00:00
|
|
|
for (i = 0; i < to->eccbytes; i++)
|
|
|
|
to->eccpos[i] = from->eccpos[i];
|
|
|
|
|
|
|
|
for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
|
|
|
|
if (from->oobfree[i].length == 0 &&
|
|
|
|
from->oobfree[i].offset == 0)
|
|
|
|
break;
|
|
|
|
to->oobavail += from->oobfree[i].length;
|
|
|
|
to->oobfree[i] = from->oobfree[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
|
2010-09-17 10:31:42 +00:00
|
|
|
struct blkpg_ioctl_arg __user *arg)
|
|
|
|
{
|
|
|
|
struct blkpg_ioctl_arg a;
|
|
|
|
struct blkpg_partition p;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
switch (a.op) {
|
|
|
|
case BLKPG_ADD_PARTITION:
|
|
|
|
|
2010-11-23 12:17:17 +00:00
|
|
|
/* Only master mtd device must be used to add partitions */
|
|
|
|
if (mtd_is_partition(mtd))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-09-17 10:31:42 +00:00
|
|
|
return mtd_add_partition(mtd, p.devname, p.start, p.length);
|
|
|
|
|
|
|
|
case BLKPG_DEL_PARTITION:
|
|
|
|
|
|
|
|
if (p.pno < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return mtd_del_partition(mtd, p.pno);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_write_ioctl(struct mtd_info *mtd,
|
2011-09-09 16:59:03 +00:00
|
|
|
struct mtd_write_req __user *argp)
|
|
|
|
{
|
|
|
|
struct mtd_write_req req;
|
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
void __user *usr_data, *usr_oob;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (copy_from_user(&req, argp, sizeof(req)) ||
|
|
|
|
!access_ok(VERIFY_READ, req.usr_data, req.len) ||
|
|
|
|
!access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
|
|
|
|
return -EFAULT;
|
2012-01-30 12:58:32 +00:00
|
|
|
if (!mtd->_write_oob)
|
2011-09-09 16:59:03 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
ops.mode = req.mode;
|
|
|
|
ops.len = (size_t)req.len;
|
|
|
|
ops.ooblen = (size_t)req.ooblen;
|
|
|
|
ops.ooboffs = 0;
|
|
|
|
|
|
|
|
usr_data = (void __user *)(uintptr_t)req.usr_data;
|
|
|
|
usr_oob = (void __user *)(uintptr_t)req.usr_oob;
|
|
|
|
|
|
|
|
if (req.usr_data) {
|
|
|
|
ops.datbuf = memdup_user(usr_data, ops.len);
|
|
|
|
if (IS_ERR(ops.datbuf))
|
|
|
|
return PTR_ERR(ops.datbuf);
|
|
|
|
} else {
|
|
|
|
ops.datbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.usr_oob) {
|
|
|
|
ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
|
|
|
|
if (IS_ERR(ops.oobbuf)) {
|
|
|
|
kfree(ops.datbuf);
|
|
|
|
return PTR_ERR(ops.oobbuf);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ops.oobbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-23 16:29:55 +00:00
|
|
|
ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
|
2011-09-09 16:59:03 +00:00
|
|
|
|
|
|
|
kfree(ops.datbuf);
|
|
|
|
kfree(ops.oobbuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-05-29 22:37:34 +00:00
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2005-04-16 22:20:36 +00:00
|
|
|
void __user *argp = (void __user *)arg;
|
|
|
|
int ret = 0;
|
|
|
|
u_long size;
|
2006-05-30 12:25:35 +00:00
|
|
|
struct mtd_info_user info;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2011-07-19 17:06:09 +00:00
|
|
|
pr_debug("MTD_ioctl\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
|
|
|
|
if (cmd & IOC_IN) {
|
|
|
|
if (!access_ok(VERIFY_READ, argp, size))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
if (cmd & IOC_OUT) {
|
|
|
|
if (!access_ok(VERIFY_WRITE, argp, size))
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case MEMGETREGIONCOUNT:
|
|
|
|
if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEMGETREGIONINFO:
|
|
|
|
{
|
2008-09-01 12:02:12 +00:00
|
|
|
uint32_t ur_idx;
|
|
|
|
struct mtd_erase_region_info *kr;
|
2010-01-15 18:25:38 +00:00
|
|
|
struct region_info_user __user *ur = argp;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-09-01 12:02:12 +00:00
|
|
|
if (get_user(ur_idx, &(ur->regionindex)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2010-09-08 19:39:56 +00:00
|
|
|
if (ur_idx >= mtd->numeraseregions)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-09-01 12:02:12 +00:00
|
|
|
kr = &(mtd->eraseregions[ur_idx]);
|
|
|
|
|
|
|
|
if (put_user(kr->offset, &(ur->offset))
|
|
|
|
|| put_user(kr->erasesize, &(ur->erasesize))
|
|
|
|
|| put_user(kr->numblocks, &(ur->numblocks)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
2008-09-01 12:02:12 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMGETINFO:
|
2010-11-06 14:41:24 +00:00
|
|
|
memset(&info, 0, sizeof(info));
|
2006-05-30 12:25:35 +00:00
|
|
|
info.type = mtd->type;
|
|
|
|
info.flags = mtd->flags;
|
|
|
|
info.size = mtd->size;
|
|
|
|
info.erasesize = mtd->erasesize;
|
|
|
|
info.writesize = mtd->writesize;
|
|
|
|
info.oobsize = mtd->oobsize;
|
2011-08-31 01:45:46 +00:00
|
|
|
/* The below field is obsolete */
|
|
|
|
info.padding = 0;
|
2006-05-30 12:25:35 +00:00
|
|
|
if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEMERASE:
|
2009-04-09 05:52:28 +00:00
|
|
|
case MEMERASE64:
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct erase_info *erase;
|
|
|
|
|
2008-09-02 19:28:45 +00:00
|
|
|
if(!(file->f_mode & FMODE_WRITE))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EPERM;
|
|
|
|
|
2006-11-15 19:10:29 +00:00
|
|
|
erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!erase)
|
|
|
|
ret = -ENOMEM;
|
|
|
|
else {
|
|
|
|
wait_queue_head_t waitq;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
|
|
|
|
init_waitqueue_head(&waitq);
|
|
|
|
|
2009-04-09 05:52:28 +00:00
|
|
|
if (cmd == MEMERASE64) {
|
|
|
|
struct erase_info_user64 einfo64;
|
|
|
|
|
|
|
|
if (copy_from_user(&einfo64, argp,
|
|
|
|
sizeof(struct erase_info_user64))) {
|
|
|
|
kfree(erase);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
erase->addr = einfo64.start;
|
|
|
|
erase->len = einfo64.length;
|
|
|
|
} else {
|
|
|
|
struct erase_info_user einfo32;
|
|
|
|
|
|
|
|
if (copy_from_user(&einfo32, argp,
|
|
|
|
sizeof(struct erase_info_user))) {
|
|
|
|
kfree(erase);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
erase->addr = einfo32.start;
|
|
|
|
erase->len = einfo32.length;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
erase->mtd = mtd;
|
|
|
|
erase->callback = mtdchar_erase_callback;
|
|
|
|
erase->priv = (unsigned long)&waitq;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
FIXME: Allow INTERRUPTIBLE. Which means
|
|
|
|
not having the wait_queue head on the stack.
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
If the wq_head is on the stack, and we
|
|
|
|
leave because we got interrupted, then the
|
|
|
|
wq_head is no longer there when the
|
|
|
|
callback routine tries to wake us up.
|
|
|
|
*/
|
2011-12-23 13:25:39 +00:00
|
|
|
ret = mtd_erase(mtd, erase);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!ret) {
|
|
|
|
set_current_state(TASK_UNINTERRUPTIBLE);
|
|
|
|
add_wait_queue(&waitq, &wait);
|
|
|
|
if (erase->state != MTD_ERASE_DONE &&
|
|
|
|
erase->state != MTD_ERASE_FAILED)
|
|
|
|
schedule();
|
|
|
|
remove_wait_queue(&waitq, &wait);
|
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
|
|
|
|
ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
|
|
|
|
}
|
|
|
|
kfree(erase);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMWRITEOOB:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf buf;
|
2009-04-09 05:53:13 +00:00
|
|
|
struct mtd_oob_buf __user *buf_user = argp;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-04-09 05:53:13 +00:00
|
|
|
/* NOTE: writes return length to buf_user->length */
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
2005-04-16 22:20:36 +00:00
|
|
|
ret = -EFAULT;
|
2009-04-09 05:53:13 +00:00
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
|
2009-04-09 05:53:13 +00:00
|
|
|
buf.ptr, &buf_user->length);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMREADOOB:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf buf;
|
2009-04-09 05:53:13 +00:00
|
|
|
struct mtd_oob_buf __user *buf_user = argp;
|
2006-05-29 01:26:58 +00:00
|
|
|
|
2009-04-09 05:53:13 +00:00
|
|
|
/* NOTE: writes return length to buf_user->start */
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
2005-04-16 22:20:36 +00:00
|
|
|
ret = -EFAULT;
|
2009-04-09 05:53:13 +00:00
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
|
2009-04-09 05:53:13 +00:00
|
|
|
buf.ptr, &buf_user->start);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-09 05:53:49 +00:00
|
|
|
case MEMWRITEOOB64:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf64 buf;
|
|
|
|
struct mtd_oob_buf64 __user *buf_user = argp;
|
|
|
|
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
|
2009-04-09 05:53:49 +00:00
|
|
|
(void __user *)(uintptr_t)buf.usr_ptr,
|
|
|
|
&buf_user->length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMREADOOB64:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf64 buf;
|
|
|
|
struct mtd_oob_buf64 __user *buf_user = argp;
|
|
|
|
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
|
2009-04-09 05:53:49 +00:00
|
|
|
(void __user *)(uintptr_t)buf.usr_ptr,
|
|
|
|
&buf_user->length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-09 16:59:03 +00:00
|
|
|
case MEMWRITE:
|
|
|
|
{
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_write_ioctl(mtd,
|
2011-09-09 16:59:03 +00:00
|
|
|
(struct mtd_write_req __user *)arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
case MEMLOCK:
|
|
|
|
{
|
2008-07-04 06:40:14 +00:00
|
|
|
struct erase_info_user einfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-07-04 06:40:14 +00:00
|
|
|
if (copy_from_user(&einfo, argp, sizeof(einfo)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2011-12-30 15:00:35 +00:00
|
|
|
ret = mtd_lock(mtd, einfo.start, einfo.length);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMUNLOCK:
|
|
|
|
{
|
2008-07-04 06:40:14 +00:00
|
|
|
struct erase_info_user einfo;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-07-04 06:40:14 +00:00
|
|
|
if (copy_from_user(&einfo, argp, sizeof(einfo)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2011-12-30 15:00:35 +00:00
|
|
|
ret = mtd_unlock(mtd, einfo.start, einfo.length);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-14 16:10:33 +00:00
|
|
|
case MEMISLOCKED:
|
|
|
|
{
|
|
|
|
struct erase_info_user einfo;
|
|
|
|
|
|
|
|
if (copy_from_user(&einfo, argp, sizeof(einfo)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2011-12-30 15:00:35 +00:00
|
|
|
ret = mtd_is_locked(mtd, einfo.start, einfo.length);
|
2010-06-14 16:10:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-05-27 20:16:10 +00:00
|
|
|
/* Legacy interface */
|
2005-04-16 22:20:36 +00:00
|
|
|
case MEMGETOOBSEL:
|
|
|
|
{
|
2006-05-27 20:16:10 +00:00
|
|
|
struct nand_oobinfo oi;
|
|
|
|
|
|
|
|
if (!mtd->ecclayout)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
oi.useecc = MTD_NANDECC_AUTOPLACE;
|
|
|
|
memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
|
|
|
|
memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
|
|
|
|
sizeof(oi.oobfree));
|
2006-10-17 15:27:11 +00:00
|
|
|
oi.eccbytes = mtd->ecclayout->eccbytes;
|
2006-05-27 20:16:10 +00:00
|
|
|
|
|
|
|
if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMGETBADBLOCK:
|
|
|
|
{
|
|
|
|
loff_t offs;
|
2005-11-07 11:15:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (copy_from_user(&offs, argp, sizeof(loff_t)))
|
|
|
|
return -EFAULT;
|
2012-01-02 11:48:54 +00:00
|
|
|
return mtd_block_isbad(mtd, offs);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMSETBADBLOCK:
|
|
|
|
{
|
|
|
|
loff_t offs;
|
|
|
|
|
|
|
|
if (copy_from_user(&offs, argp, sizeof(loff_t)))
|
|
|
|
return -EFAULT;
|
2012-01-02 11:59:12 +00:00
|
|
|
return mtd_block_markbad(mtd, offs);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-30 19:35:05 +00:00
|
|
|
#ifdef CONFIG_HAVE_MTD_OTP
|
2005-02-08 17:45:55 +00:00
|
|
|
case OTPSELECT:
|
|
|
|
{
|
|
|
|
int mode;
|
|
|
|
if (copy_from_user(&mode, argp, sizeof(int)))
|
|
|
|
return -EFAULT;
|
2006-05-29 22:37:34 +00:00
|
|
|
|
2011-08-31 01:45:41 +00:00
|
|
|
mfi->mode = MTD_FILE_MODE_NORMAL;
|
2006-05-29 22:37:34 +00:00
|
|
|
|
|
|
|
ret = otp_select_filemode(mfi, mode);
|
|
|
|
|
2005-04-01 15:36:15 +00:00
|
|
|
file->f_pos = 0;
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OTPGETREGIONCOUNT:
|
|
|
|
case OTPGETREGIONINFO:
|
|
|
|
{
|
|
|
|
struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
2006-05-29 22:37:34 +00:00
|
|
|
switch (mfi->mode) {
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_FACTORY:
|
2011-12-28 16:47:46 +00:00
|
|
|
ret = mtd_get_fact_prot_info(mtd, buf, 4096);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_USER:
|
2011-12-28 16:47:46 +00:00
|
|
|
ret = mtd_get_user_prot_info(mtd, buf, 4096);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
2006-05-29 22:37:34 +00:00
|
|
|
default:
|
2011-12-28 16:47:46 +00:00
|
|
|
ret = -EINVAL;
|
2006-05-29 22:37:34 +00:00
|
|
|
break;
|
2005-02-08 17:45:55 +00:00
|
|
|
}
|
|
|
|
if (ret >= 0) {
|
|
|
|
if (cmd == OTPGETREGIONCOUNT) {
|
|
|
|
int nbr = ret / sizeof(struct otp_info);
|
|
|
|
ret = copy_to_user(argp, &nbr, sizeof(int));
|
|
|
|
} else
|
|
|
|
ret = copy_to_user(argp, buf, ret);
|
|
|
|
if (ret)
|
|
|
|
ret = -EFAULT;
|
|
|
|
}
|
|
|
|
kfree(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OTPLOCK:
|
|
|
|
{
|
2008-07-04 06:40:14 +00:00
|
|
|
struct otp_info oinfo;
|
2005-02-08 17:45:55 +00:00
|
|
|
|
2011-08-31 01:45:41 +00:00
|
|
|
if (mfi->mode != MTD_FILE_MODE_OTP_USER)
|
2005-02-08 17:45:55 +00:00
|
|
|
return -EINVAL;
|
2008-07-04 06:40:14 +00:00
|
|
|
if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
|
2005-02-08 17:45:55 +00:00
|
|
|
return -EFAULT;
|
2011-12-23 16:55:49 +00:00
|
|
|
ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
|
2005-02-08 17:45:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-23 21:12:08 +00:00
|
|
|
/* This ioctl is being deprecated - it truncates the ECC layout */
|
2006-05-29 22:37:34 +00:00
|
|
|
case ECCGETLAYOUT:
|
|
|
|
{
|
2010-08-25 01:12:00 +00:00
|
|
|
struct nand_ecclayout_user *usrlay;
|
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
if (!mtd->ecclayout)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2010-08-25 01:12:00 +00:00
|
|
|
usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
|
|
|
|
if (!usrlay)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
shrink_ecclayout(mtd->ecclayout, usrlay);
|
|
|
|
|
|
|
|
if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
kfree(usrlay);
|
2006-05-29 22:37:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ECCGETSTATS:
|
|
|
|
{
|
|
|
|
if (copy_to_user(argp, &mtd->ecc_stats,
|
|
|
|
sizeof(struct mtd_ecc_stats)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MTDFILEMODE:
|
|
|
|
{
|
|
|
|
mfi->mode = 0;
|
|
|
|
|
|
|
|
switch(arg) {
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_OTP_FACTORY:
|
|
|
|
case MTD_FILE_MODE_OTP_USER:
|
2006-05-29 22:37:34 +00:00
|
|
|
ret = otp_select_filemode(mfi, arg);
|
|
|
|
break;
|
|
|
|
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_RAW:
|
2011-12-28 16:35:07 +00:00
|
|
|
if (!mtd_has_oob(mtd))
|
2006-05-29 22:37:34 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
mfi->mode = arg;
|
|
|
|
|
2011-08-31 01:45:41 +00:00
|
|
|
case MTD_FILE_MODE_NORMAL:
|
2006-05-29 22:37:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
file->f_pos = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-17 10:31:42 +00:00
|
|
|
case BLKPG:
|
|
|
|
{
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_blkpg_ioctl(mtd,
|
2010-09-17 10:31:42 +00:00
|
|
|
(struct blkpg_ioctl_arg __user *)arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BLKRRPART:
|
|
|
|
{
|
|
|
|
/* No reread partition feature. Just return ok */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
} /* memory_ioctl */
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
|
2010-04-26 22:24:05 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_lock(&mtd_mutex);
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_ioctl(file, cmd, arg);
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_unlock(&mtd_mutex);
|
2010-04-26 22:24:05 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-09 05:53:13 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
|
|
|
|
struct mtd_oob_buf32 {
|
|
|
|
u_int32_t start;
|
|
|
|
u_int32_t length;
|
|
|
|
compat_caddr_t ptr; /* unsigned char* */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
|
|
|
|
#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
|
|
|
|
|
2011-12-23 15:27:46 +00:00
|
|
|
static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
|
2009-04-09 05:53:13 +00:00
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2009-05-29 15:09:08 +00:00
|
|
|
void __user *argp = compat_ptr(arg);
|
2009-04-09 05:53:13 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_lock(&mtd_mutex);
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MEMWRITEOOB32:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf32 buf;
|
|
|
|
struct mtd_oob_buf32 __user *buf_user = argp;
|
|
|
|
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_writeoob(file, mtd, buf.start,
|
2009-04-09 05:53:13 +00:00
|
|
|
buf.length, compat_ptr(buf.ptr),
|
|
|
|
&buf_user->length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MEMREADOOB32:
|
|
|
|
{
|
|
|
|
struct mtd_oob_buf32 buf;
|
|
|
|
struct mtd_oob_buf32 __user *buf_user = argp;
|
|
|
|
|
|
|
|
/* NOTE: writes return length to buf->start */
|
|
|
|
if (copy_from_user(&buf, argp, sizeof(buf)))
|
|
|
|
ret = -EFAULT;
|
|
|
|
else
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_readoob(file, mtd, buf.start,
|
2009-04-09 05:53:13 +00:00
|
|
|
buf.length, compat_ptr(buf.ptr),
|
|
|
|
&buf_user->start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2011-12-23 15:27:46 +00:00
|
|
|
ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
|
2009-04-09 05:53:13 +00:00
|
|
|
}
|
|
|
|
|
2010-06-02 12:28:52 +00:00
|
|
|
mutex_unlock(&mtd_mutex);
|
2009-04-09 05:53:13 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_COMPAT */
|
|
|
|
|
2009-02-12 10:40:00 +00:00
|
|
|
/*
|
|
|
|
* try to determine where a shared mapping can be made
|
|
|
|
* - only supported for NOMMU at the moment (MMU can't doesn't copy private
|
|
|
|
* mappings)
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_MMU
|
2011-12-23 15:27:46 +00:00
|
|
|
static unsigned long mtdchar_get_unmapped_area(struct file *file,
|
2009-02-12 10:40:00 +00:00
|
|
|
unsigned long addr,
|
|
|
|
unsigned long len,
|
|
|
|
unsigned long pgoff,
|
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2011-12-30 12:31:57 +00:00
|
|
|
unsigned long offset;
|
|
|
|
int ret;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
2011-12-30 12:31:57 +00:00
|
|
|
if (addr != 0)
|
|
|
|
return (unsigned long) -EINVAL;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
2011-12-30 12:31:57 +00:00
|
|
|
if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
|
|
|
|
return (unsigned long) -EINVAL;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
2011-12-30 12:31:57 +00:00
|
|
|
offset = pgoff << PAGE_SHIFT;
|
|
|
|
if (offset > mtd->size - len)
|
|
|
|
return (unsigned long) -EINVAL;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
2011-12-30 12:31:57 +00:00
|
|
|
ret = mtd_get_unmapped_area(mtd, len, offset, flags);
|
|
|
|
return ret == -EOPNOTSUPP ? -ENOSYS : ret;
|
2009-02-12 10:40:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-08 19:57:30 +00:00
|
|
|
static inline unsigned long get_vm_size(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return vma->vm_end - vma->vm_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline resource_size_t get_vm_offset(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
return (resource_size_t) vma->vm_pgoff << PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a new vm offset.
|
|
|
|
*
|
|
|
|
* Verify that the incoming offset really works as a page offset,
|
|
|
|
* and that the offset and size fit in a resource_size_t.
|
|
|
|
*/
|
|
|
|
static inline int set_vm_offset(struct vm_area_struct *vma, resource_size_t off)
|
|
|
|
{
|
|
|
|
pgoff_t pgoff = off >> PAGE_SHIFT;
|
|
|
|
if (off != (resource_size_t) pgoff << PAGE_SHIFT)
|
|
|
|
return -EINVAL;
|
|
|
|
if (off + get_vm_size(vma) - 1 < off)
|
|
|
|
return -EINVAL;
|
|
|
|
vma->vm_pgoff = pgoff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-12 10:40:00 +00:00
|
|
|
/*
|
|
|
|
* set up a mapping for shared memory segments
|
|
|
|
*/
|
2011-12-23 15:27:46 +00:00
|
|
|
static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
|
2009-02-12 10:40:00 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
struct mtd_file_info *mfi = file->private_data;
|
|
|
|
struct mtd_info *mtd = mfi->mtd;
|
2010-06-15 07:30:15 +00:00
|
|
|
struct map_info *map = mtd->priv;
|
2012-09-08 19:57:30 +00:00
|
|
|
resource_size_t start, off;
|
|
|
|
unsigned long len, vma_len;
|
2010-06-15 07:30:15 +00:00
|
|
|
|
2012-10-09 14:08:10 +00:00
|
|
|
/* This is broken because it assumes the MTD device is map-based
|
|
|
|
and that mtd->priv is a valid struct map_info. It should be
|
|
|
|
replaced with something that uses the mtd_get_unmapped_area()
|
|
|
|
operation properly. */
|
|
|
|
if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
|
2012-09-08 19:57:30 +00:00
|
|
|
off = get_vm_offset(vma);
|
2010-06-15 07:30:15 +00:00
|
|
|
start = map->phys;
|
|
|
|
len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
|
|
|
|
start &= PAGE_MASK;
|
2012-09-08 19:57:30 +00:00
|
|
|
vma_len = get_vm_size(vma);
|
|
|
|
|
|
|
|
/* Overflow in off+len? */
|
|
|
|
if (vma_len + off < off)
|
|
|
|
return -EINVAL;
|
|
|
|
/* Does it fit in the mapping? */
|
|
|
|
if (vma_len + off > len)
|
2010-06-15 07:30:15 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
off += start;
|
2012-09-08 19:57:30 +00:00
|
|
|
/* Did that overflow? */
|
|
|
|
if (off < start)
|
|
|
|
return -EINVAL;
|
|
|
|
if (set_vm_offset(vma, off) < 0)
|
|
|
|
return -EINVAL;
|
mm: kill vma flag VM_RESERVED and mm->reserved_vm counter
A long time ago, in v2.4, VM_RESERVED kept swapout process off VMA,
currently it lost original meaning but still has some effects:
| effect | alternative flags
-+------------------------+---------------------------------------------
1| account as reserved_vm | VM_IO
2| skip in core dump | VM_IO, VM_DONTDUMP
3| do not merge or expand | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
4| do not mlock | VM_IO, VM_DONTEXPAND, VM_HUGETLB, VM_PFNMAP
This patch removes reserved_vm counter from mm_struct. Seems like nobody
cares about it, it does not exported into userspace directly, it only
reduces total_vm showed in proc.
Thus VM_RESERVED can be replaced with VM_IO or pair VM_DONTEXPAND | VM_DONTDUMP.
remap_pfn_range() and io_remap_pfn_range() set VM_IO|VM_DONTEXPAND|VM_DONTDUMP.
remap_vmalloc_range() set VM_DONTEXPAND | VM_DONTDUMP.
[akpm@linux-foundation.org: drivers/vfio/pci/vfio_pci.c fixup]
Signed-off-by: Konstantin Khlebnikov <khlebnikov@openvz.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Carsten Otte <cotte@de.ibm.com>
Cc: Chris Metcalf <cmetcalf@tilera.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Eric Paris <eparis@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Morris <james.l.morris@oracle.com>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Kentaro Takeda <takedakn@nttdata.co.jp>
Cc: Matt Helsley <matthltc@us.ibm.com>
Cc: Nick Piggin <npiggin@kernel.dk>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Robert Richter <robert.richter@amd.com>
Cc: Suresh Siddha <suresh.b.siddha@intel.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Venkatesh Pallipadi <venki@google.com>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-08 23:29:02 +00:00
|
|
|
vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
|
2010-06-15 07:30:15 +00:00
|
|
|
|
|
|
|
#ifdef pgprot_noncached
|
|
|
|
if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
|
|
|
|
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
|
|
|
#endif
|
|
|
|
if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
|
|
|
|
vma->vm_end - vma->vm_start,
|
|
|
|
vma->vm_page_prot))
|
|
|
|
return -EAGAIN;
|
2009-02-12 10:40:00 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-06-15 07:30:15 +00:00
|
|
|
}
|
2009-02-12 10:40:00 +00:00
|
|
|
return -ENOSYS;
|
|
|
|
#else
|
|
|
|
return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-02-12 08:55:34 +00:00
|
|
|
static const struct file_operations mtd_fops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.owner = THIS_MODULE,
|
2011-12-23 15:27:46 +00:00
|
|
|
.llseek = mtdchar_lseek,
|
|
|
|
.read = mtdchar_read,
|
|
|
|
.write = mtdchar_write,
|
|
|
|
.unlocked_ioctl = mtdchar_unlocked_ioctl,
|
2009-04-09 05:53:13 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
2011-12-23 15:27:46 +00:00
|
|
|
.compat_ioctl = mtdchar_compat_ioctl,
|
2009-04-09 05:53:13 +00:00
|
|
|
#endif
|
2011-12-23 15:27:46 +00:00
|
|
|
.open = mtdchar_open,
|
|
|
|
.release = mtdchar_close,
|
|
|
|
.mmap = mtdchar_mmap,
|
2009-02-12 10:40:00 +00:00
|
|
|
#ifndef CONFIG_MMU
|
2011-12-23 15:27:46 +00:00
|
|
|
.get_unmapped_area = mtdchar_get_unmapped_area,
|
2009-02-12 10:40:00 +00:00
|
|
|
#endif
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2012-03-23 17:47:52 +00:00
|
|
|
static const struct super_operations mtd_ops = {
|
|
|
|
.drop_inode = generic_delete_inode,
|
|
|
|
.statfs = simple_statfs,
|
|
|
|
};
|
|
|
|
|
2010-07-25 19:47:46 +00:00
|
|
|
static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
2010-05-17 13:55:47 +00:00
|
|
|
{
|
2012-03-23 17:47:52 +00:00
|
|
|
return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
|
2010-05-17 13:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type mtd_inodefs_type = {
|
|
|
|
.name = "mtd_inodefs",
|
2010-07-25 19:47:46 +00:00
|
|
|
.mount = mtd_inodefs_mount,
|
2010-05-17 13:55:47 +00:00
|
|
|
.kill_sb = kill_anon_super,
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int __init init_mtdchar(void)
|
|
|
|
{
|
2010-05-17 13:55:47 +00:00
|
|
|
int ret;
|
2009-03-26 07:42:41 +00:00
|
|
|
|
2010-05-17 13:55:47 +00:00
|
|
|
ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
|
2010-01-29 21:00:04 +00:00
|
|
|
"mtd", &mtd_fops);
|
2010-05-17 13:55:47 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
pr_notice("Can't allocate major number %d for "
|
|
|
|
"Memory Technology Devices.\n", MTD_CHAR_MAJOR);
|
|
|
|
return ret;
|
2005-06-30 00:23:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-17 13:55:47 +00:00
|
|
|
ret = register_filesystem(&mtd_inodefs_type);
|
|
|
|
if (ret) {
|
|
|
|
pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
|
|
|
|
goto err_unregister_chdev;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err_unregister_chdev:
|
|
|
|
__unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
|
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit cleanup_mtdchar(void)
|
|
|
|
{
|
2010-05-17 13:55:47 +00:00
|
|
|
unregister_filesystem(&mtd_inodefs_type);
|
2010-01-29 21:00:04 +00:00
|
|
|
__unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(init_mtdchar);
|
|
|
|
module_exit(cleanup_mtdchar);
|
|
|
|
|
2009-03-26 07:42:41 +00:00
|
|
|
MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
|
|
|
|
MODULE_DESCRIPTION("Direct character-device access to MTD devices");
|
2009-03-02 18:42:39 +00:00
|
|
|
MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
|