Files

1207 lines
26 KiB
C
Raw Permalink Normal View History

2005-04-16 15:20:36 -07:00
/*
2010-08-08 20:58:20 +01: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 15:20:36 -07:00
*
*/
2005-11-07 00:14:42 +01:00
#include <linux/device.h>
#include <linux/fs.h>
2007-08-10 13:01:06 -07:00
#include <linux/mm.h>
#include <linux/err.h>
2005-11-07 00:14:42 +01:00
#include <linux/init.h>
2005-04-16 15:20:36 -07:00
#include <linux/kernel.h>
#include <linux/module.h>
2005-11-07 00:14:42 +01:00
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/mutex.h>
#include <linux/backing-dev.h>
2009-04-08 22:53:13 -07:00
#include <linux/compat.h>
#include <linux/mount.h>
#include <linux/blkpg.h>
#include <linux/magic.h>
#include <linux/major.h>
2005-04-16 15:20:36 -07:00
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/map.h>
2005-04-16 15:20:36 -07:00
#include <linux/uaccess.h>
2005-06-30 01:23:27 +01:00
2013-03-14 13:27:40 +02:00
#include "mtdcore.h"
static DEFINE_MUTEX(mtd_mutex);
2005-04-16 15:20:36 -07:00
2005-02-08 19:12:53 +00:00
/*
* Data structure to hold the pointer to the mtd device as well
2011-07-20 09:53:42 -07:00
* as mode information of various use cases.
2005-02-08 19:12:53 +00:00
*/
struct mtd_file_info {
struct mtd_info *mtd;
enum mtd_file_modes mode;
};
2011-12-23 17:27:46 +02:00
static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
2005-04-16 15:20:36 -07:00
{
struct mtd_file_info *mfi = file->private_data;
2013-06-16 20:27:42 +04:00
return fixed_size_llseek(file, offset, orig, mfi->mtd->size);
2005-04-16 15:20:36 -07:00
}
2011-12-23 17:27:46 +02:00
static int mtdchar_open(struct inode *inode, struct file *file)
2005-04-16 15:20:36 -07:00
{
int minor = iminor(inode);
int devnum = minor >> 1;
2008-05-15 10:10:37 -06:00
int ret = 0;
2005-04-16 15:20:36 -07:00
struct mtd_info *mtd;
struct mtd_file_info *mfi;
2005-04-16 15:20:36 -07:00
2011-07-19 10:06:09 -07:00
pr_debug("MTD_open\n");
2005-04-16 15:20:36 -07:00
/* You can't open the RO devices RW */
2008-09-02 15:28:45 -04:00
if ((file->f_mode & FMODE_WRITE) && (minor & 1))
2005-04-16 15:20:36 -07:00
return -EACCES;
mutex_lock(&mtd_mutex);
2005-04-16 15:20:36 -07:00
mtd = get_mtd_device(NULL, devnum);
2005-11-07 11:15:26 +00:00
2008-05-15 10:10:37 -06:00
if (IS_ERR(mtd)) {
ret = PTR_ERR(mtd);
goto out;
}
2005-11-07 11:15:26 +00:00
if (mtd->type == MTD_ABSENT) {
2008-05-15 10:10:37 -06:00
ret = -ENODEV;
goto out1;
2005-04-16 15:20:36 -07:00
}
/* You can't open it RW if it's not a writeable device */
2008-09-02 15:28:45 -04:00
if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
2008-05-15 10:10:37 -06:00
ret = -EACCES;
goto out1;
2005-04-16 15:20:36 -07:00
}
2005-11-07 11:15:26 +00:00
mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
if (!mfi) {
2008-05-15 10:10:37 -06:00
ret = -ENOMEM;
goto out1;
}
mfi->mtd = mtd;
file->private_data = mfi;
mutex_unlock(&mtd_mutex);
return 0;
out1:
put_mtd_device(mtd);
2008-05-15 10:10:37 -06:00
out:
mutex_unlock(&mtd_mutex);
2008-05-15 10:10:37 -06:00
return ret;
2011-12-23 17:27:46 +02:00
} /* mtdchar_open */
2005-04-16 15:20:36 -07:00
/*====================================================================*/
2011-12-23 17:27:46 +02:00
static int mtdchar_close(struct inode *inode, struct file *file)
2005-04-16 15:20:36 -07:00
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
2005-04-16 15:20:36 -07:00
2011-07-19 10:06:09 -07:00
pr_debug("MTD_close\n");
2005-04-16 15:20:36 -07:00
/* Only sync if opened RW */
2011-12-30 16:35:35 +02:00
if ((file->f_mode & FMODE_WRITE))
2011-12-23 19:03:12 +02:00
mtd_sync(mtd);
2005-11-07 11:15:26 +00:00
2005-04-16 15:20:36 -07:00
put_mtd_device(mtd);
file->private_data = NULL;
kfree(mfi);
2005-04-16 15:20:36 -07:00
return 0;
2011-12-23 17:27:46 +02:00
} /* mtdchar_close */
2005-04-16 15:20:36 -07: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 15:20:36 -07:00
2011-12-23 17:27:46 +02:00
static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
2005-04-16 15:20:36 -07:00
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
2011-12-29 15:16:28 +02:00
size_t retlen;
2005-04-16 15:20:36 -07:00
size_t total_retlen=0;
int ret=0;
int len;
size_t size = count;
2005-04-16 15:20:36 -07:00
char *kbuf;
2005-11-07 11:15:26 +00:00
2011-07-19 10:06:09 -07:00
pr_debug("MTD_read\n");
2005-04-16 15:20:36 -07:00
if (*ppos + count > mtd->size) {
if (*ppos < mtd->size)
count = mtd->size - *ppos;
else
count = 0;
}
2005-04-16 15:20:36 -07:00
if (!count)
return 0;
2005-11-07 11:15:26 +00:00
kbuf = mtd_kmalloc_up_to(mtd, &size);
if (!kbuf)
return -ENOMEM;
2005-04-16 15:20:36 -07:00
while (count) {
len = min_t(size_t, count, size);
2005-04-16 15:20:36 -07:00
switch (mfi->mode) {
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_FACTORY:
ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
&retlen, kbuf);
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_USER:
ret = mtd_read_user_prot_reg(mtd, *ppos, len,
&retlen, kbuf);
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_RAW:
{
struct mtd_oob_ops ops;
2011-08-30 18:45:40 -07:00
ops.mode = MTD_OPS_RAW;
ops.datbuf = kbuf;
ops.oobbuf = NULL;
ops.len = len;
2011-12-23 18:27:05 +02:00
ret = mtd_read_oob(mtd, *ppos, &ops);
retlen = ops.retlen;
break;
}
default:
2011-12-23 17:30:16 +02:00
ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
}
2011-06-23 14:12:08 -07:00
/* Nand returns -EBADMSG on ECC errors, but it returns
2005-04-16 15:20:36 -07:00
* the data. For our userspace tools it is important
2011-06-23 14:12:08 -07:00
* to dump areas with ECC errors!
* For kernel internal usage it also might return -EUCLEAN
2011-03-30 22:57:33 -03:00
* to signal the caller that a bitflip has occurred and has
* been corrected by the ECC algorithm.
2005-04-16 15:20:36 -07:00
* Userspace software which accesses NAND this way
* must be aware of the fact that it deals with NAND
*/
2011-09-20 18:34:25 -07:00
if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
2005-04-16 15:20:36 -07:00
*ppos += retlen;
if (copy_to_user(buf, kbuf, retlen)) {
2006-05-28 11:01:53 +02:00
kfree(kbuf);
2005-04-16 15:20:36 -07:00
return -EFAULT;
}
else
total_retlen += retlen;
count -= retlen;
buf += retlen;
if (retlen == 0)
count = 0;
2005-04-16 15:20:36 -07:00
}
else {
kfree(kbuf);
return ret;
}
2005-11-07 11:15:26 +00:00
2005-04-16 15:20:36 -07:00
}
kfree(kbuf);
2005-04-16 15:20:36 -07:00
return total_retlen;
2011-12-23 17:27:46 +02:00
} /* mtdchar_read */
2005-04-16 15:20:36 -07:00
2011-12-23 17:27:46 +02:00
static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
loff_t *ppos)
2005-04-16 15:20:36 -07:00
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
size_t size = count;
2005-04-16 15:20:36 -07:00
char *kbuf;
size_t retlen;
size_t total_retlen=0;
int ret=0;
int len;
2011-07-19 10:06:09 -07:00
pr_debug("MTD_write\n");
2005-11-07 11:15:26 +00:00
if (*ppos >= mtd->size)
2005-04-16 15:20:36 -07:00
return -ENOSPC;
2005-11-07 11:15:26 +00:00
2005-04-16 15:20:36 -07:00
if (*ppos + count > mtd->size)
count = mtd->size - *ppos;
if (!count)
return 0;
kbuf = mtd_kmalloc_up_to(mtd, &size);
if (!kbuf)
return -ENOMEM;
2005-04-16 15:20:36 -07:00
while (count) {
len = min_t(size_t, count, size);
2005-04-16 15:20:36 -07:00
if (copy_from_user(kbuf, buf, len)) {
kfree(kbuf);
return -EFAULT;
}
2005-11-07 11:15:26 +00:00
switch (mfi->mode) {
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_FACTORY:
ret = -EROFS;
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_USER:
ret = mtd_write_user_prot_reg(mtd, *ppos, len,
&retlen, kbuf);
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_RAW:
{
struct mtd_oob_ops ops;
2011-08-30 18:45:40 -07:00
ops.mode = MTD_OPS_RAW;
ops.datbuf = kbuf;
ops.oobbuf = NULL;
ops.ooboffs = 0;
ops.len = len;
2011-12-23 18:29:55 +02:00
ret = mtd_write_oob(mtd, *ppos, &ops);
retlen = ops.retlen;
break;
}
default:
2011-12-23 17:35:41 +02:00
ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
}
/*
* Return -ENOSPC only if no data could be written at all.
* Otherwise just return the number of bytes that actually
* have been written.
*/
if ((ret == -ENOSPC) && (total_retlen))
break;
2005-04-16 15:20:36 -07:00
if (!ret) {
*ppos += retlen;
total_retlen += retlen;
count -= retlen;
buf += retlen;
}
else {
kfree(kbuf);
return ret;
}
}
kfree(kbuf);
2005-04-16 15:20:36 -07:00
return total_retlen;
2011-12-23 17:27:46 +02:00
} /* mtdchar_write */
2005-04-16 15:20:36 -07:00
/*======================================================================
IOCTL calls for getting device parameters.
======================================================================*/
static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
{
struct mtd_info *mtd = mfi->mtd;
size_t retlen;
switch (mode) {
case MTD_OTP_FACTORY:
if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
-EOPNOTSUPP)
return -EOPNOTSUPP;
mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
break;
case MTD_OTP_USER:
if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
-EOPNOTSUPP)
return -EOPNOTSUPP;
mfi->mode = MTD_FILE_MODE_OTP_USER;
break;
case MTD_OTP_OFF:
mfi->mode = MTD_FILE_MODE_NORMAL;
break;
default:
return -EINVAL;
}
return 0;
}
2011-12-23 17:27:46 +02:00
static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
2009-04-08 22:53:13 -07:00
uint64_t start, uint32_t length, void __user *ptr,
uint32_t __user *retp)
{
2011-08-30 18:45:37 -07:00
struct mtd_file_info *mfi = file->private_data;
2009-04-08 22:53:13 -07: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;
if (!mtd->_write_oob)
2017-09-29 13:55:19 -04:00
return -EOPNOTSUPP;
2009-04-08 22:53:13 -07:00
ops.ooblen = length;
2011-08-23 17:17:32 -07:00
ops.ooboffs = start & (mtd->writesize - 1);
2009-04-08 22:53:13 -07:00
ops.datbuf = NULL;
2011-08-30 18:45:41 -07:00
ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
2011-08-30 18:45:40 -07:00
MTD_OPS_PLACE_OOB;
2009-04-08 22:53:13 -07:00
if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
return -EINVAL;
2010-05-22 10:22:49 +02:00
ops.oobbuf = memdup_user(ptr, length);
if (IS_ERR(ops.oobbuf))
return PTR_ERR(ops.oobbuf);
2009-04-08 22:53:13 -07:00
2011-08-23 17:17:32 -07:00
start &= ~((uint64_t)mtd->writesize - 1);
2011-12-23 18:29:55 +02:00
ret = mtd_write_oob(mtd, start, &ops);
2009-04-08 22:53:13 -07: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 17:27:46 +02:00
static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
2011-08-30 18:45:38 -07:00
uint64_t start, uint32_t length, void __user *ptr,
uint32_t __user *retp)
2009-04-08 22:53:13 -07:00
{
2011-08-30 18:45:38 -07:00
struct mtd_file_info *mfi = file->private_data;
2009-04-08 22:53:13 -07:00
struct mtd_oob_ops ops;
int ret = 0;
if (length > 4096)
return -EINVAL;
ops.ooblen = length;
2011-08-23 17:17:32 -07:00
ops.ooboffs = start & (mtd->writesize - 1);
2009-04-08 22:53:13 -07:00
ops.datbuf = NULL;
2011-08-30 18:45:41 -07:00
ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
2011-08-30 18:45:40 -07:00
MTD_OPS_PLACE_OOB;
2009-04-08 22:53:13 -07:00
if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
return -EINVAL;
ops.oobbuf = kmalloc(length, GFP_KERNEL);
if (!ops.oobbuf)
return -ENOMEM;
2011-08-23 17:17:32 -07:00
start &= ~((uint64_t)mtd->writesize - 1);
2011-12-23 18:27:05 +02:00
ret = mtd_read_oob(mtd, start, &ops);
2009-04-08 22:53:13 -07: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 16:45:24 -07: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
2017-01-27 10:36:38 +09:00
* to signal the caller that a bitflip has occurred and has
2011-06-23 16:45:24 -07:00
* been corrected by the ECC algorithm.
*
2011-06-28 16:29:00 -07: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 16:45:24 -07:00
*/
2011-09-20 18:34:25 -07:00
if (mtd_is_bitflip_or_eccerr(ret))
2011-06-23 16:45:24 -07:00
return 0;
2009-04-08 22:53:13 -07:00
return ret;
}
/*
2016-02-04 10:16:18 +01:00
* Copies (and truncates, if necessary) OOB layout information to the
* deprecated layout struct, nand_ecclayout_user. This is necessary only to
* support the deprecated API ioctl ECCGETLAYOUT while allowing all new
* functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
* can describe any kind of OOB layout with almost zero overhead from a
* memory usage point of view).
*/
static int shrink_ecclayout(struct mtd_info *mtd,
struct nand_ecclayout_user *to)
{
struct mtd_oob_region oobregion;
int i, section = 0, ret;
if (!mtd || !to)
return -EINVAL;
memset(to, 0, sizeof(*to));
to->eccbytes = 0;
for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
u32 eccpos;
2018-03-11 15:59:07 +08:00
ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
if (ret < 0) {
if (ret != -ERANGE)
return ret;
break;
}
eccpos = oobregion.offset;
for (; i < MTD_MAX_ECCPOS_ENTRIES &&
eccpos < oobregion.offset + oobregion.length; i++) {
to->eccpos[i] = eccpos++;
to->eccbytes++;
}
}
for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
ret = mtd_ooblayout_free(mtd, i, &oobregion);
if (ret < 0) {
if (ret != -ERANGE)
return ret;
break;
}
to->oobfree[i].offset = oobregion.offset;
to->oobfree[i].length = oobregion.length;
to->oobavail += to->oobfree[i].length;
}
return 0;
}
static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
{
struct mtd_oob_region oobregion;
int i, section = 0, ret;
if (!mtd || !to)
return -EINVAL;
memset(to, 0, sizeof(*to));
to->eccbytes = 0;
for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
u32 eccpos;
2018-03-11 15:59:07 +08:00
ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
if (ret < 0) {
if (ret != -ERANGE)
return ret;
break;
}
if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
return -EINVAL;
eccpos = oobregion.offset;
for (; eccpos < oobregion.offset + oobregion.length; i++) {
to->eccpos[i] = eccpos++;
to->eccbytes++;
}
}
for (i = 0; i < 8; i++) {
ret = mtd_ooblayout_free(mtd, i, &oobregion);
if (ret < 0) {
if (ret != -ERANGE)
return ret;
break;
}
to->oobfree[i][0] = oobregion.offset;
to->oobfree[i][1] = oobregion.length;
}
to->useecc = MTD_NANDECC_AUTOPLACE;
return 0;
}
2011-12-23 17:27:46 +02:00
static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
struct blkpg_ioctl_arg *arg)
{
struct blkpg_partition p;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (copy_from_user(&p, arg->data, sizeof(p)))
return -EFAULT;
switch (arg->op) {
case BLKPG_ADD_PARTITION:
/* Only master mtd device must be used to add partitions */
if (mtd_is_partition(mtd))
return -EINVAL;
2014-07-21 19:08:13 -07:00
/* Sanitize user input */
p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
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 17:27:46 +02:00
static int mtdchar_write_ioctl(struct mtd_info *mtd,
2011-09-09 09:59:03 -07:00
struct mtd_write_req __user *argp)
{
struct mtd_write_req req;
struct mtd_oob_ops ops;
const void __user *usr_data, *usr_oob;
2011-09-09 09:59:03 -07:00
int ret;
if (copy_from_user(&req, argp, sizeof(req)))
2011-09-09 09:59:03 -07:00
return -EFAULT;
usr_data = (const void __user *)(uintptr_t)req.usr_data;
usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
if (!mtd->_write_oob)
2011-09-09 09:59:03 -07:00
return -EOPNOTSUPP;
ops.mode = req.mode;
ops.len = (size_t)req.len;
ops.ooblen = (size_t)req.ooblen;
ops.ooboffs = 0;
if (usr_data) {
2011-09-09 09:59:03 -07:00
ops.datbuf = memdup_user(usr_data, ops.len);
if (IS_ERR(ops.datbuf))
return PTR_ERR(ops.datbuf);
} else {
ops.datbuf = NULL;
}
if (usr_oob) {
2011-09-09 09:59:03 -07:00
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 18:29:55 +02:00
ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
2011-09-09 09:59:03 -07:00
kfree(ops.datbuf);
kfree(ops.oobbuf);
return ret;
}
2011-12-23 17:27:46 +02:00
static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
2005-04-16 15:20:36 -07:00
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
2005-04-16 15:20:36 -07:00
void __user *argp = (void __user *)arg;
int ret = 0;
2006-05-30 14:25:35 +02:00
struct mtd_info_user info;
2005-11-07 11:15:26 +00:00
2011-07-19 10:06:09 -07:00
pr_debug("MTD_ioctl\n");
2005-04-16 15:20:36 -07:00
switch (cmd) {
case MEMGETREGIONCOUNT:
if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
return -EFAULT;
break;
case MEMGETREGIONINFO:
{
uint32_t ur_idx;
struct mtd_erase_region_info *kr;
2010-01-15 11:25:38 -07:00
struct region_info_user __user *ur = argp;
2005-04-16 15:20:36 -07:00
if (get_user(ur_idx, &(ur->regionindex)))
2005-04-16 15:20:36 -07:00
return -EFAULT;
2010-09-08 21:39:56 +02:00
if (ur_idx >= mtd->numeraseregions)
return -EINVAL;
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 15:20:36 -07:00
return -EFAULT;
2005-04-16 15:20:36 -07:00
break;
}
case MEMGETINFO:
memset(&info, 0, sizeof(info));
2006-05-30 14:25:35 +02: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;
/* The below field is obsolete */
info.padding = 0;
2006-05-30 14:25:35 +02:00
if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
2005-04-16 15:20:36 -07:00
return -EFAULT;
break;
case MEMERASE:
2009-04-08 22:52:28 -07:00
case MEMERASE64:
2005-04-16 15:20:36 -07:00
{
struct erase_info *erase;
2008-09-02 15:28:45 -04:00
if(!(file->f_mode & FMODE_WRITE))
2005-04-16 15:20:36 -07:00
return -EPERM;
2006-11-15 21:10:29 +02:00
erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
2005-04-16 15:20:36 -07:00
if (!erase)
ret = -ENOMEM;
else {
2009-04-08 22:52:28 -07: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 15:20:36 -07:00
}
2005-11-07 11:15:26 +00:00
2011-12-23 15:25:39 +02:00
ret = mtd_erase(mtd, erase);
2005-04-16 15:20:36 -07:00
kfree(erase);
}
break;
}
case MEMWRITEOOB:
{
struct mtd_oob_buf buf;
2009-04-08 22:53:13 -07:00
struct mtd_oob_buf __user *buf_user = argp;
2005-11-07 11:15:26 +00:00
2009-04-08 22:53:13 -07:00
/* NOTE: writes return length to buf_user->length */
if (copy_from_user(&buf, argp, sizeof(buf)))
2005-04-16 15:20:36 -07:00
ret = -EFAULT;
2009-04-08 22:53:13 -07:00
else
2011-12-23 17:27:46 +02:00
ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
2009-04-08 22:53:13 -07:00
buf.ptr, &buf_user->length);
2005-04-16 15:20:36 -07:00
break;
}
case MEMREADOOB:
{
struct mtd_oob_buf buf;
2009-04-08 22:53:13 -07:00
struct mtd_oob_buf __user *buf_user = argp;
2005-04-16 15:20:36 -07:00
2009-04-08 22:53:13 -07:00
/* NOTE: writes return length to buf_user->start */
if (copy_from_user(&buf, argp, sizeof(buf)))
ret = -EFAULT;
2005-04-16 15:20:36 -07:00
else
2011-12-23 17:27:46 +02:00
ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
2009-04-08 22:53:13 -07:00
buf.ptr, &buf_user->start);
2005-04-16 15:20:36 -07:00
break;
}
2009-04-08 22:53:49 -07: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 17:27:46 +02:00
ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
2009-04-08 22:53:49 -07: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 17:27:46 +02:00
ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
2009-04-08 22:53:49 -07:00
(void __user *)(uintptr_t)buf.usr_ptr,
&buf_user->length);
break;
}
2011-09-09 09:59:03 -07:00
case MEMWRITE:
{
2011-12-23 17:27:46 +02:00
ret = mtdchar_write_ioctl(mtd,
2011-09-09 09:59:03 -07:00
(struct mtd_write_req __user *)arg);
break;
}
2005-04-16 15:20:36 -07:00
case MEMLOCK:
{
struct erase_info_user einfo;
2005-04-16 15:20:36 -07:00
if (copy_from_user(&einfo, argp, sizeof(einfo)))
2005-04-16 15:20:36 -07:00
return -EFAULT;
ret = mtd_lock(mtd, einfo.start, einfo.length);
2005-04-16 15:20:36 -07:00
break;
}
case MEMUNLOCK:
{
struct erase_info_user einfo;
2005-04-16 15:20:36 -07:00
if (copy_from_user(&einfo, argp, sizeof(einfo)))
2005-04-16 15:20:36 -07:00
return -EFAULT;
ret = mtd_unlock(mtd, einfo.start, einfo.length);
2005-04-16 15:20:36 -07:00
break;
}
case MEMISLOCKED:
{
struct erase_info_user einfo;
if (copy_from_user(&einfo, argp, sizeof(einfo)))
return -EFAULT;
ret = mtd_is_locked(mtd, einfo.start, einfo.length);
break;
}
2006-05-27 22:16:10 +02:00
/* Legacy interface */
2005-04-16 15:20:36 -07:00
case MEMGETOOBSEL:
{
2006-05-27 22:16:10 +02:00
struct nand_oobinfo oi;
if (!mtd->ooblayout)
2006-05-27 22:16:10 +02:00
return -EOPNOTSUPP;
ret = get_oobinfo(mtd, &oi);
if (ret)
return ret;
2006-05-27 22:16:10 +02:00
if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
2005-04-16 15:20:36 -07:00
return -EFAULT;
break;
}
case MEMGETBADBLOCK:
{
loff_t offs;
2005-11-07 11:15:26 +00:00
2005-04-16 15:20:36 -07:00
if (copy_from_user(&offs, argp, sizeof(loff_t)))
return -EFAULT;
2012-01-02 13:48:54 +02:00
return mtd_block_isbad(mtd, offs);
2005-04-16 15:20:36 -07:00
break;
}
case MEMSETBADBLOCK:
{
loff_t offs;
if (copy_from_user(&offs, argp, sizeof(loff_t)))
return -EFAULT;
2012-01-02 13:59:12 +02:00
return mtd_block_markbad(mtd, offs);
2005-04-16 15:20:36 -07:00
break;
}
case OTPSELECT:
{
int mode;
if (copy_from_user(&mode, argp, sizeof(int)))
return -EFAULT;
2011-08-30 18:45:41 -07:00
mfi->mode = MTD_FILE_MODE_NORMAL;
ret = otp_select_filemode(mfi, mode);
file->f_pos = 0;
break;
}
case OTPGETREGIONCOUNT:
case OTPGETREGIONINFO:
{
struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
size_t retlen;
if (!buf)
return -ENOMEM;
switch (mfi->mode) {
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_FACTORY:
ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_USER:
ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
break;
default:
ret = -EINVAL;
break;
}
if (!ret) {
if (cmd == OTPGETREGIONCOUNT) {
int nbr = retlen / sizeof(struct otp_info);
ret = copy_to_user(argp, &nbr, sizeof(int));
} else
ret = copy_to_user(argp, buf, retlen);
if (ret)
ret = -EFAULT;
}
kfree(buf);
break;
}
case OTPLOCK:
{
struct otp_info oinfo;
2011-08-30 18:45:41 -07:00
if (mfi->mode != MTD_FILE_MODE_OTP_USER)
return -EINVAL;
if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
return -EFAULT;
ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
break;
}
2011-06-23 14:12:08 -07:00
/* This ioctl is being deprecated - it truncates the ECC layout */
case ECCGETLAYOUT:
{
struct nand_ecclayout_user *usrlay;
if (!mtd->ooblayout)
return -EOPNOTSUPP;
usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
if (!usrlay)
return -ENOMEM;
shrink_ecclayout(mtd, usrlay);
if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
ret = -EFAULT;
kfree(usrlay);
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-30 18:45:41 -07:00
case MTD_FILE_MODE_OTP_FACTORY:
case MTD_FILE_MODE_OTP_USER:
ret = otp_select_filemode(mfi, arg);
break;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_RAW:
2011-12-28 18:35:07 +02:00
if (!mtd_has_oob(mtd))
return -EOPNOTSUPP;
mfi->mode = arg;
2011-08-30 18:45:41 -07:00
case MTD_FILE_MODE_NORMAL:
break;
default:
ret = -EINVAL;
}
file->f_pos = 0;
break;
}
case BLKPG:
{
struct blkpg_ioctl_arg __user *blk_arg = argp;
struct blkpg_ioctl_arg a;
if (copy_from_user(&a, blk_arg, sizeof(a)))
ret = -EFAULT;
else
ret = mtdchar_blkpg_ioctl(mtd, &a);
break;
}
case BLKRRPART:
{
/* No reread partition feature. Just return ok */
ret = 0;
break;
}
2005-04-16 15:20:36 -07:00
default:
ret = -ENOTTY;
}
return ret;
} /* memory_ioctl */
2011-12-23 17:27:46 +02:00
static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
2010-04-27 00:24:05 +02:00
{
int ret;
mutex_lock(&mtd_mutex);
2011-12-23 17:27:46 +02:00
ret = mtdchar_ioctl(file, cmd, arg);
mutex_unlock(&mtd_mutex);
2010-04-27 00:24:05 +02:00
return ret;
}
2009-04-08 22:53:13 -07: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 17:27:46 +02:00
static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
2009-04-08 22:53:13 -07:00
unsigned long arg)
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
void __user *argp = compat_ptr(arg);
2009-04-08 22:53:13 -07:00
int ret = 0;
mutex_lock(&mtd_mutex);
2009-04-08 22:53:13 -07: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 17:27:46 +02:00
ret = mtdchar_writeoob(file, mtd, buf.start,
2009-04-08 22:53:13 -07: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 17:27:46 +02:00
ret = mtdchar_readoob(file, mtd, buf.start,
2009-04-08 22:53:13 -07:00
buf.length, compat_ptr(buf.ptr),
&buf_user->start);
break;
}
case BLKPG:
{
/* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
struct blkpg_compat_ioctl_arg __user *uarg = argp;
struct blkpg_compat_ioctl_arg compat_arg;
struct blkpg_ioctl_arg a;
if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
ret = -EFAULT;
break;
}
memset(&a, 0, sizeof(a));
a.op = compat_arg.op;
a.flags = compat_arg.flags;
a.datalen = compat_arg.datalen;
a.data = compat_ptr(compat_arg.data);
ret = mtdchar_blkpg_ioctl(mtd, &a);
break;
}
2009-04-08 22:53:13 -07:00
default:
2011-12-23 17:27:46 +02:00
ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
2009-04-08 22:53:13 -07:00
}
mutex_unlock(&mtd_mutex);
2009-04-08 22:53:13 -07:00
return ret;
}
#endif /* CONFIG_COMPAT */
/*
* 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 17:27:46 +02:00
static unsigned long mtdchar_get_unmapped_area(struct file *file,
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;
unsigned long offset;
int ret;
if (addr != 0)
return (unsigned long) -EINVAL;
if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
return (unsigned long) -EINVAL;
offset = pgoff << PAGE_SHIFT;
if (offset > mtd->size - len)
return (unsigned long) -EINVAL;
ret = mtd_get_unmapped_area(mtd, len, offset, flags);
return ret == -EOPNOTSUPP ? -ENODEV : ret;
}
static unsigned mtdchar_mmap_capabilities(struct file *file)
{
struct mtd_file_info *mfi = file->private_data;
return mtd_mmap_capabilities(mfi->mtd);
}
#endif
/*
* set up a mapping for shared memory segments
*/
2011-12-23 17:27:46 +02:00
static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
{
#ifdef CONFIG_MMU
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
struct map_info *map = mtd->priv;
2012-10-09 15:08:10 +01: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*/) {
#ifdef pgprot_noncached
if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
#endif
return vm_iomap_memory(vma, map->phys, map->size);
}
return -ENODEV;
#else
return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
#endif
}
2007-02-12 00:55:34 -08:00
static const struct file_operations mtd_fops = {
2005-04-16 15:20:36 -07:00
.owner = THIS_MODULE,
2011-12-23 17:27:46 +02:00
.llseek = mtdchar_lseek,
.read = mtdchar_read,
.write = mtdchar_write,
.unlocked_ioctl = mtdchar_unlocked_ioctl,
2009-04-08 22:53:13 -07:00
#ifdef CONFIG_COMPAT
2011-12-23 17:27:46 +02:00
.compat_ioctl = mtdchar_compat_ioctl,
2009-04-08 22:53:13 -07:00
#endif
2011-12-23 17:27:46 +02:00
.open = mtdchar_open,
.release = mtdchar_close,
.mmap = mtdchar_mmap,
#ifndef CONFIG_MMU
2011-12-23 17:27:46 +02:00
.get_unmapped_area = mtdchar_get_unmapped_area,
.mmap_capabilities = mtdchar_mmap_capabilities,
#endif
2005-04-16 15:20:36 -07:00
};
2013-03-14 13:27:40 +02:00
int __init init_mtdchar(void)
2005-04-16 15:20:36 -07:00
{
int ret;
2009-03-26 00:42:41 -07:00
ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
"mtd", &mtd_fops);
if (ret < 0) {
2013-03-15 12:59:36 +02:00
pr_err("Can't allocate major number %d for MTD\n",
MTD_CHAR_MAJOR);
return ret;
2005-04-16 15:20:36 -07:00
}
return ret;
2005-04-16 15:20:36 -07:00
}
2013-03-14 13:27:40 +02:00
void __exit cleanup_mtdchar(void)
2005-04-16 15:20:36 -07:00
{
__unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
2005-04-16 15:20:36 -07:00
}
MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);