blob: 684bfa39e4ee4892f901874c52c699fa71fd572a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Woodhousea1452a32010-08-08 20:58:20 +01002 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 */
19
Thomas Gleixner15fdc522005-11-07 00:14:42 +010020#include <linux/device.h>
21#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -070022#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030023#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010024#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010027#include <linux/slab.h>
28#include <linux/sched.h>
Arnd Bergmann5aa82942010-06-02 14:28:52 +020029#include <linux/mutex.h>
David Howells402d3262009-02-12 10:40:00 +000030#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070031#include <linux/compat.h>
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030032#include <linux/mount.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030033#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070034#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/mtd/mtd.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030036#include <linux/mtd/partitions.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020037#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Thomas Gleixner15fdc522005-11-07 00:14:42 +010039#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010040
Artem Bityutskiy660685d2013-03-14 13:27:40 +020041#include "mtdcore.h"
42
Arnd Bergmann5aa82942010-06-02 14:28:52 +020043static DEFINE_MUTEX(mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Nicolas Pitre045e9a52005-02-08 19:12:53 +000045/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020046 * Data structure to hold the pointer to the mtd device as well
Brian Norris92394b5c2011-07-20 09:53:42 -070047 * as mode information of various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000048 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020049struct mtd_file_info {
50 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030051 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052 enum mtd_file_modes mode;
53};
Nicolas Pitre31f42332005-02-08 17:45:55 +000054
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020055static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020057 struct mtd_file_info *mfi = file->private_data;
Al Virob9599572013-06-16 20:27:42 +040058 return fixed_size_llseek(file, offset, orig, mfi->mtd->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Al Viro00292bb2012-03-23 13:47:52 -040061static int count;
62static struct vfsmount *mnt;
63static struct file_system_type mtd_inodefs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020065static int mtdchar_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 int minor = iminor(inode);
68 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060069 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020071 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030072 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Brian Norris289c0522011-07-19 10:06:09 -070074 pr_debug("MTD_open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040077 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EACCES;
79
Al Viro00292bb2012-03-23 13:47:52 -040080 ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
81 if (ret)
82 return ret;
83
Arnd Bergmann5aa82942010-06-02 14:28:52 +020084 mutex_lock(&mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000086
Jonathan Corbet60712392008-05-15 10:10:37 -060087 if (IS_ERR(mtd)) {
88 ret = PTR_ERR(mtd);
89 goto out;
90 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000091
David Howells402d3262009-02-12 10:40:00 +000092 if (mtd->type == MTD_ABSENT) {
Jonathan Corbet60712392008-05-15 10:10:37 -060093 ret = -ENODEV;
Al Viroc65390f2012-04-09 01:36:28 -040094 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96
Al Viro00292bb2012-03-23 13:47:52 -040097 mtd_ino = iget_locked(mnt->mnt_sb, devnum);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030098 if (!mtd_ino) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030099 ret = -ENOMEM;
Al Viroc65390f2012-04-09 01:36:28 -0400100 goto out1;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300101 }
102 if (mtd_ino->i_state & I_NEW) {
103 mtd_ino->i_private = mtd;
104 mtd_ino->i_mode = S_IFCHR;
105 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
106 unlock_new_inode(mtd_ino);
107 }
108 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400111 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Jonathan Corbet60712392008-05-15 10:10:37 -0600112 ret = -EACCES;
Al Viroc65390f2012-04-09 01:36:28 -0400113 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000115
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200116 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
117 if (!mfi) {
Jonathan Corbet60712392008-05-15 10:10:37 -0600118 ret = -ENOMEM;
Al Viroc65390f2012-04-09 01:36:28 -0400119 goto out2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200120 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300121 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200122 mfi->mtd = mtd;
123 file->private_data = mfi;
Al Viroc65390f2012-04-09 01:36:28 -0400124 mutex_unlock(&mtd_mutex);
125 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200126
Al Viroc65390f2012-04-09 01:36:28 -0400127out2:
128 iput(mtd_ino);
129out1:
130 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600131out:
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200132 mutex_unlock(&mtd_mutex);
Al Viro00292bb2012-03-23 13:47:52 -0400133 simple_release_fs(&mnt, &count);
Jonathan Corbet60712392008-05-15 10:10:37 -0600134 return ret;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200135} /* mtdchar_open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*====================================================================*/
138
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200139static int mtdchar_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200141 struct mtd_file_info *mfi = file->private_data;
142 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Brian Norris289c0522011-07-19 10:06:09 -0700144 pr_debug("MTD_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200146 /* Only sync if opened RW */
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200147 if ((file->f_mode & FMODE_WRITE))
Artem Bityutskiy85f2f2a2011-12-23 19:03:12 +0200148 mtd_sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000149
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300150 iput(mfi->ino);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200153 file->private_data = NULL;
154 kfree(mfi);
Al Viro00292bb2012-03-23 13:47:52 -0400155 simple_release_fs(&mnt, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return 0;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200158} /* mtdchar_close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Grant Erickson3e45cf52011-04-08 08:51:33 -0700160/* Back in June 2001, dwmw2 wrote:
161 *
162 * FIXME: This _really_ needs to die. In 2.5, we should lock the
163 * userspace buffer down and use it directly with readv/writev.
164 *
165 * The implementation below, using mtd_kmalloc_up_to, mitigates
166 * allocation failures when the system is under low-memory situations
167 * or if memory is highly fragmented at the cost of reducing the
168 * performance of the requested transfer due to a smaller buffer size.
169 *
170 * A more complex but more memory-efficient implementation based on
171 * get_user_pages and iovecs to cover extents of those pages is a
172 * longer-term goal, as intimated by dwmw2 above. However, for the
173 * write case, this requires yet more complex head and tail transfer
174 * handling when those head and tail offsets and sizes are such that
175 * alignment requirements are not met in the NAND subdriver.
176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200178static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
179 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200181 struct mtd_file_info *mfi = file->private_data;
182 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200183 size_t retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 size_t total_retlen=0;
185 int ret=0;
186 int len;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700187 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000189
Brian Norris289c0522011-07-19 10:06:09 -0700190 pr_debug("MTD_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (*ppos + count > mtd->size)
193 count = mtd->size - *ppos;
194
195 if (!count)
196 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000197
Grant Erickson3e45cf52011-04-08 08:51:33 -0700198 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100199 if (!kbuf)
200 return -ENOMEM;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700203 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200205 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700206 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200207 ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
208 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000209 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700210 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy4ea1cab2011-12-23 18:47:59 +0200211 ret = mtd_read_user_prot_reg(mtd, *ppos, len,
212 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000213 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700214 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200215 {
216 struct mtd_oob_ops ops;
217
Brian Norris0612b9d2011-08-30 18:45:40 -0700218 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200219 ops.datbuf = kbuf;
220 ops.oobbuf = NULL;
221 ops.len = len;
222
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200223 ret = mtd_read_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200224 retlen = ops.retlen;
225 break;
226 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000227 default:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200228 ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000229 }
Brian Norris7854d3f2011-06-23 14:12:08 -0700230 /* Nand returns -EBADMSG on ECC errors, but it returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * the data. For our userspace tools it is important
Brian Norris7854d3f2011-06-23 14:12:08 -0700232 * to dump areas with ECC errors!
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200233 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300234 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200235 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Userspace software which accesses NAND this way
237 * must be aware of the fact that it deals with NAND
238 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700239 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *ppos += retlen;
241 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200242 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EFAULT;
244 }
245 else
246 total_retlen += retlen;
247
248 count -= retlen;
249 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000250 if (retlen == 0)
251 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 else {
254 kfree(kbuf);
255 return ret;
256 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Thago Galesib802c072006-04-17 17:38:15 +0100260 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200262} /* mtdchar_read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200264static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
265 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200267 struct mtd_file_info *mfi = file->private_data;
268 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700269 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 char *kbuf;
271 size_t retlen;
272 size_t total_retlen=0;
273 int ret=0;
274 int len;
275
Brian Norris289c0522011-07-19 10:06:09 -0700276 pr_debug("MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (*ppos == mtd->size)
279 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (*ppos + count > mtd->size)
282 count = mtd->size - *ppos;
283
284 if (!count)
285 return 0;
286
Grant Erickson3e45cf52011-04-08 08:51:33 -0700287 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100288 if (!kbuf)
289 return -ENOMEM;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700292 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (copy_from_user(kbuf, buf, len)) {
295 kfree(kbuf);
296 return -EFAULT;
297 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000298
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200299 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700300 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000301 ret = -EROFS;
302 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700303 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy482b43a2011-12-23 18:50:04 +0200304 ret = mtd_write_user_prot_reg(mtd, *ppos, len,
305 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000306 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200307
Brian Norrisbeb133f2011-08-30 18:45:41 -0700308 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200309 {
310 struct mtd_oob_ops ops;
311
Brian Norris0612b9d2011-08-30 18:45:40 -0700312 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200313 ops.datbuf = kbuf;
314 ops.oobbuf = NULL;
Peter Wippichbf514082011-06-06 15:50:58 +0200315 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200316 ops.len = len;
317
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200318 ret = mtd_write_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200319 retlen = ops.retlen;
320 break;
321 }
322
Nicolas Pitre31f42332005-02-08 17:45:55 +0000323 default:
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200324 ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!ret) {
327 *ppos += retlen;
328 total_retlen += retlen;
329 count -= retlen;
330 buf += retlen;
331 }
332 else {
333 kfree(kbuf);
334 return ret;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Thago Galesib802c072006-04-17 17:38:15 +0100338 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200340} /* mtdchar_write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/*======================================================================
343
344 IOCTL calls for getting device parameters.
345
346======================================================================*/
347static void mtdchar_erase_callback (struct erase_info *instr)
348{
349 wake_up((wait_queue_head_t *)instr->priv);
350}
351
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200352static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
353{
354 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200355 size_t retlen;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200356
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200357 switch (mode) {
358 case MTD_OTP_FACTORY:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100359 if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
360 -EOPNOTSUPP)
361 return -EOPNOTSUPP;
362
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200363 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200364 break;
365 case MTD_OTP_USER:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100366 if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
367 -EOPNOTSUPP)
368 return -EOPNOTSUPP;
369
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200370 mfi->mode = MTD_FILE_MODE_OTP_USER;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200371 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200372 case MTD_OTP_OFF:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100373 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200374 break;
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100375 default:
376 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200377 }
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100378
379 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200380}
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200381
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200382static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
Kevin Cernekee97718542009-04-08 22:53:13 -0700383 uint64_t start, uint32_t length, void __user *ptr,
384 uint32_t __user *retp)
385{
Brian Norris9ce244b2011-08-30 18:45:37 -0700386 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700387 struct mtd_oob_ops ops;
388 uint32_t retlen;
389 int ret = 0;
390
391 if (!(file->f_mode & FMODE_WRITE))
392 return -EPERM;
393
394 if (length > 4096)
395 return -EINVAL;
396
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200397 if (!mtd->_write_oob)
Kevin Cernekee97718542009-04-08 22:53:13 -0700398 ret = -EOPNOTSUPP;
399 else
Roel Kluin00404762010-01-29 10:35:04 +0100400 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700401
402 if (ret)
403 return ret;
404
405 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700406 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700407 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700408 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700409 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700410
411 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
412 return -EINVAL;
413
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200414 ops.oobbuf = memdup_user(ptr, length);
415 if (IS_ERR(ops.oobbuf))
416 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700417
Brian Norris305b93f2011-08-23 17:17:32 -0700418 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200419 ret = mtd_write_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700420
421 if (ops.oobretlen > 0xFFFFFFFFU)
422 ret = -EOVERFLOW;
423 retlen = ops.oobretlen;
424 if (copy_to_user(retp, &retlen, sizeof(length)))
425 ret = -EFAULT;
426
427 kfree(ops.oobbuf);
428 return ret;
429}
430
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200431static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
Brian Norrisc46f6482011-08-30 18:45:38 -0700432 uint64_t start, uint32_t length, void __user *ptr,
433 uint32_t __user *retp)
Kevin Cernekee97718542009-04-08 22:53:13 -0700434{
Brian Norrisc46f6482011-08-30 18:45:38 -0700435 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700436 struct mtd_oob_ops ops;
437 int ret = 0;
438
439 if (length > 4096)
440 return -EINVAL;
441
Artem Bityutskiydac26392011-12-28 17:50:34 +0200442 if (!access_ok(VERIFY_WRITE, ptr, length))
443 return -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700444
445 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700446 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700447 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700448 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700449 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700450
451 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
452 return -EINVAL;
453
454 ops.oobbuf = kmalloc(length, GFP_KERNEL);
455 if (!ops.oobbuf)
456 return -ENOMEM;
457
Brian Norris305b93f2011-08-23 17:17:32 -0700458 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200459 ret = mtd_read_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700460
461 if (put_user(ops.oobretlen, retp))
462 ret = -EFAULT;
463 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
464 ops.oobretlen))
465 ret = -EFAULT;
466
467 kfree(ops.oobbuf);
Brian Norris041e4572011-06-23 16:45:24 -0700468
469 /*
470 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
471 * data. For our userspace tools it is important to dump areas
472 * with ECC errors!
473 * For kernel internal usage it also might return -EUCLEAN
474 * to signal the caller that a bitflip has occured and has
475 * been corrected by the ECC algorithm.
476 *
Brian Norrisc478d7e2011-06-28 16:29:00 -0700477 * Note: currently the standard NAND function, nand_read_oob_std,
478 * does not calculate ECC for the OOB area, so do not rely on
479 * this behavior unless you have replaced it with your own.
Brian Norris041e4572011-06-23 16:45:24 -0700480 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700481 if (mtd_is_bitflip_or_eccerr(ret))
Brian Norris041e4572011-06-23 16:45:24 -0700482 return 0;
483
Kevin Cernekee97718542009-04-08 22:53:13 -0700484 return ret;
485}
486
Brian Norriscc26c3c2010-08-24 18:12:00 -0700487/*
488 * Copies (and truncates, if necessary) data from the larger struct,
489 * nand_ecclayout, to the smaller, deprecated layout struct,
Brian Norris92394b5c2011-07-20 09:53:42 -0700490 * nand_ecclayout_user. This is necessary only to support the deprecated
Brian Norriscc26c3c2010-08-24 18:12:00 -0700491 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
492 * nand_ecclayout flexibly (i.e. the struct may change size in new
493 * releases without requiring major rewrites).
494 */
495static int shrink_ecclayout(const struct nand_ecclayout *from,
496 struct nand_ecclayout_user *to)
497{
498 int i;
499
500 if (!from || !to)
501 return -EINVAL;
502
503 memset(to, 0, sizeof(*to));
504
Brian Norris0ceacf32010-09-19 23:57:12 -0700505 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700506 for (i = 0; i < to->eccbytes; i++)
507 to->eccpos[i] = from->eccpos[i];
508
509 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
510 if (from->oobfree[i].length == 0 &&
511 from->oobfree[i].offset == 0)
512 break;
513 to->oobavail += from->oobfree[i].length;
514 to->oobfree[i] = from->oobfree[i];
515 }
516
517 return 0;
518}
519
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200520static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300521 struct blkpg_ioctl_arg __user *arg)
522{
523 struct blkpg_ioctl_arg a;
524 struct blkpg_partition p;
525
526 if (!capable(CAP_SYS_ADMIN))
527 return -EPERM;
528
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300529 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
530 return -EFAULT;
531
532 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
533 return -EFAULT;
534
535 switch (a.op) {
536 case BLKPG_ADD_PARTITION:
537
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200538 /* Only master mtd device must be used to add partitions */
539 if (mtd_is_partition(mtd))
540 return -EINVAL;
541
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300542 return mtd_add_partition(mtd, p.devname, p.start, p.length);
543
544 case BLKPG_DEL_PARTITION:
545
546 if (p.pno < 0)
547 return -EINVAL;
548
549 return mtd_del_partition(mtd, p.pno);
550
551 default:
552 return -EINVAL;
553 }
554}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300555
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200556static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700557 struct mtd_write_req __user *argp)
558{
559 struct mtd_write_req req;
560 struct mtd_oob_ops ops;
561 void __user *usr_data, *usr_oob;
562 int ret;
563
564 if (copy_from_user(&req, argp, sizeof(req)) ||
565 !access_ok(VERIFY_READ, req.usr_data, req.len) ||
566 !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
567 return -EFAULT;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200568 if (!mtd->_write_oob)
Brian Norrise99d8b02011-09-09 09:59:03 -0700569 return -EOPNOTSUPP;
570
571 ops.mode = req.mode;
572 ops.len = (size_t)req.len;
573 ops.ooblen = (size_t)req.ooblen;
574 ops.ooboffs = 0;
575
576 usr_data = (void __user *)(uintptr_t)req.usr_data;
577 usr_oob = (void __user *)(uintptr_t)req.usr_oob;
578
579 if (req.usr_data) {
580 ops.datbuf = memdup_user(usr_data, ops.len);
581 if (IS_ERR(ops.datbuf))
582 return PTR_ERR(ops.datbuf);
583 } else {
584 ops.datbuf = NULL;
585 }
586
587 if (req.usr_oob) {
588 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
589 if (IS_ERR(ops.oobbuf)) {
590 kfree(ops.datbuf);
591 return PTR_ERR(ops.oobbuf);
592 }
593 } else {
594 ops.oobbuf = NULL;
595 }
596
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200597 ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
Brian Norrise99d8b02011-09-09 09:59:03 -0700598
599 kfree(ops.datbuf);
600 kfree(ops.oobbuf);
601
602 return ret;
603}
604
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200605static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200607 struct mtd_file_info *mfi = file->private_data;
608 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 void __user *argp = (void __user *)arg;
610 int ret = 0;
611 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200612 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000613
Brian Norris289c0522011-07-19 10:06:09 -0700614 pr_debug("MTD_ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
617 if (cmd & IOC_IN) {
618 if (!access_ok(VERIFY_READ, argp, size))
619 return -EFAULT;
620 }
621 if (cmd & IOC_OUT) {
622 if (!access_ok(VERIFY_WRITE, argp, size))
623 return -EFAULT;
624 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 switch (cmd) {
627 case MEMGETREGIONCOUNT:
628 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
629 return -EFAULT;
630 break;
631
632 case MEMGETREGIONINFO:
633 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700634 uint32_t ur_idx;
635 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700636 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Zev Weissb67c5f82008-09-01 05:02:12 -0700638 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return -EFAULT;
640
Dan Carpenter5e59be12010-09-08 21:39:56 +0200641 if (ur_idx >= mtd->numeraseregions)
642 return -EINVAL;
643
Zev Weissb67c5f82008-09-01 05:02:12 -0700644 kr = &(mtd->eraseregions[ur_idx]);
645
646 if (put_user(kr->offset, &(ur->offset))
647 || put_user(kr->erasesize, &(ur->erasesize))
648 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 }
653
654 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300655 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200656 info.type = mtd->type;
657 info.flags = mtd->flags;
658 info.size = mtd->size;
659 info.erasesize = mtd->erasesize;
660 info.writesize = mtd->writesize;
661 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700662 /* The below field is obsolete */
663 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200664 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EFAULT;
666 break;
667
668 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700669 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 {
671 struct erase_info *erase;
672
Al Viroaeb5d722008-09-02 15:28:45 -0400673 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EPERM;
675
Burman Yan95b93a02006-11-15 21:10:29 +0200676 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!erase)
678 ret = -ENOMEM;
679 else {
680 wait_queue_head_t waitq;
681 DECLARE_WAITQUEUE(wait, current);
682
683 init_waitqueue_head(&waitq);
684
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700685 if (cmd == MEMERASE64) {
686 struct erase_info_user64 einfo64;
687
688 if (copy_from_user(&einfo64, argp,
689 sizeof(struct erase_info_user64))) {
690 kfree(erase);
691 return -EFAULT;
692 }
693 erase->addr = einfo64.start;
694 erase->len = einfo64.length;
695 } else {
696 struct erase_info_user einfo32;
697
698 if (copy_from_user(&einfo32, argp,
699 sizeof(struct erase_info_user))) {
700 kfree(erase);
701 return -EFAULT;
702 }
703 erase->addr = einfo32.start;
704 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 erase->mtd = mtd;
707 erase->callback = mtdchar_erase_callback;
708 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 FIXME: Allow INTERRUPTIBLE. Which means
712 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 If the wq_head is on the stack, and we
715 leave because we got interrupted, then the
716 wq_head is no longer there when the
717 callback routine tries to wake us up.
718 */
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200719 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!ret) {
721 set_current_state(TASK_UNINTERRUPTIBLE);
722 add_wait_queue(&waitq, &wait);
723 if (erase->state != MTD_ERASE_DONE &&
724 erase->state != MTD_ERASE_FAILED)
725 schedule();
726 remove_wait_queue(&waitq, &wait);
727 set_current_state(TASK_RUNNING);
728
729 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
730 }
731 kfree(erase);
732 }
733 break;
734 }
735
736 case MEMWRITEOOB:
737 {
738 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700739 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000740
Kevin Cernekee97718542009-04-08 22:53:13 -0700741 /* NOTE: writes return length to buf_user->length */
742 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700744 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200745 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700746 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 case MEMREADOOB:
751 {
752 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700753 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Kevin Cernekee97718542009-04-08 22:53:13 -0700755 /* NOTE: writes return length to buf_user->start */
756 if (copy_from_user(&buf, argp, sizeof(buf)))
757 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200759 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700760 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
763
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700764 case MEMWRITEOOB64:
765 {
766 struct mtd_oob_buf64 buf;
767 struct mtd_oob_buf64 __user *buf_user = argp;
768
769 if (copy_from_user(&buf, argp, sizeof(buf)))
770 ret = -EFAULT;
771 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200772 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700773 (void __user *)(uintptr_t)buf.usr_ptr,
774 &buf_user->length);
775 break;
776 }
777
778 case MEMREADOOB64:
779 {
780 struct mtd_oob_buf64 buf;
781 struct mtd_oob_buf64 __user *buf_user = argp;
782
783 if (copy_from_user(&buf, argp, sizeof(buf)))
784 ret = -EFAULT;
785 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200786 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700787 (void __user *)(uintptr_t)buf.usr_ptr,
788 &buf_user->length);
789 break;
790 }
791
Brian Norrise99d8b02011-09-09 09:59:03 -0700792 case MEMWRITE:
793 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200794 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700795 (struct mtd_write_req __user *)arg);
796 break;
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 case MEMLOCK:
800 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700801 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Harvey Harrison175428b2008-07-03 23:40:14 -0700803 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EFAULT;
805
Artem Bityutskiy38134562011-12-30 17:00:35 +0200806 ret = mtd_lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 }
809
810 case MEMUNLOCK:
811 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700812 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Harvey Harrison175428b2008-07-03 23:40:14 -0700814 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -EFAULT;
816
Artem Bityutskiy38134562011-12-30 17:00:35 +0200817 ret = mtd_unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 }
820
Richard Cochran99384242010-06-14 18:10:33 +0200821 case MEMISLOCKED:
822 {
823 struct erase_info_user einfo;
824
825 if (copy_from_user(&einfo, argp, sizeof(einfo)))
826 return -EFAULT;
827
Artem Bityutskiy38134562011-12-30 17:00:35 +0200828 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
Richard Cochran99384242010-06-14 18:10:33 +0200829 break;
830 }
831
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200832 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 case MEMGETOOBSEL:
834 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200835 struct nand_oobinfo oi;
836
837 if (!mtd->ecclayout)
838 return -EOPNOTSUPP;
839 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
840 return -EINVAL;
841
842 oi.useecc = MTD_NANDECC_AUTOPLACE;
843 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
844 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
845 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200846 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200847
848 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -EFAULT;
850 break;
851 }
852
853 case MEMGETBADBLOCK:
854 {
855 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (copy_from_user(&offs, argp, sizeof(loff_t)))
858 return -EFAULT;
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200859 return mtd_block_isbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 }
862
863 case MEMSETBADBLOCK:
864 {
865 loff_t offs;
866
867 if (copy_from_user(&offs, argp, sizeof(loff_t)))
868 return -EFAULT;
Artem Bityutskiy800ffd32012-01-02 13:59:12 +0200869 return mtd_block_markbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 }
872
Nicolas Pitre31f42332005-02-08 17:45:55 +0000873 case OTPSELECT:
874 {
875 int mode;
876 if (copy_from_user(&mode, argp, sizeof(int)))
877 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200878
Brian Norrisbeb133f2011-08-30 18:45:41 -0700879 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200880
881 ret = otp_select_filemode(mfi, mode);
882
Nicolas Pitre81dba482005-04-01 16:36:15 +0100883 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000884 break;
885 }
886
887 case OTPGETREGIONCOUNT:
888 case OTPGETREGIONINFO:
889 {
890 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
891 if (!buf)
892 return -ENOMEM;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200893 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700894 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200895 ret = mtd_get_fact_prot_info(mtd, buf, 4096);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000896 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700897 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200898 ret = mtd_get_user_prot_info(mtd, buf, 4096);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000899 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200900 default:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200901 ret = -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200902 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000903 }
904 if (ret >= 0) {
905 if (cmd == OTPGETREGIONCOUNT) {
906 int nbr = ret / sizeof(struct otp_info);
907 ret = copy_to_user(argp, &nbr, sizeof(int));
908 } else
909 ret = copy_to_user(argp, buf, ret);
910 if (ret)
911 ret = -EFAULT;
912 }
913 kfree(buf);
914 break;
915 }
916
917 case OTPLOCK:
918 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700919 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000920
Brian Norrisbeb133f2011-08-30 18:45:41 -0700921 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000922 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700923 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000924 return -EFAULT;
Artem Bityutskiy4403dbfb2011-12-23 18:55:49 +0200925 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000926 break;
927 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000928
Brian Norris7854d3f2011-06-23 14:12:08 -0700929 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200930 case ECCGETLAYOUT:
931 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700932 struct nand_ecclayout_user *usrlay;
933
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200934 if (!mtd->ecclayout)
935 return -EOPNOTSUPP;
936
Brian Norriscc26c3c2010-08-24 18:12:00 -0700937 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
938 if (!usrlay)
939 return -ENOMEM;
940
941 shrink_ecclayout(mtd->ecclayout, usrlay);
942
943 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
944 ret = -EFAULT;
945 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200946 break;
947 }
948
949 case ECCGETSTATS:
950 {
951 if (copy_to_user(argp, &mtd->ecc_stats,
952 sizeof(struct mtd_ecc_stats)))
953 return -EFAULT;
954 break;
955 }
956
957 case MTDFILEMODE:
958 {
959 mfi->mode = 0;
960
961 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700962 case MTD_FILE_MODE_OTP_FACTORY:
963 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200964 ret = otp_select_filemode(mfi, arg);
965 break;
966
Brian Norrisbeb133f2011-08-30 18:45:41 -0700967 case MTD_FILE_MODE_RAW:
Artem Bityutskiyfc002e32011-12-28 18:35:07 +0200968 if (!mtd_has_oob(mtd))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200969 return -EOPNOTSUPP;
970 mfi->mode = arg;
971
Brian Norrisbeb133f2011-08-30 18:45:41 -0700972 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200973 break;
974 default:
975 ret = -EINVAL;
976 }
977 file->f_pos = 0;
978 break;
979 }
980
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300981 case BLKPG:
982 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200983 ret = mtdchar_blkpg_ioctl(mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300984 (struct blkpg_ioctl_arg __user *)arg);
985 break;
986 }
987
988 case BLKRRPART:
989 {
990 /* No reread partition feature. Just return ok */
991 ret = 0;
992 break;
993 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 default:
996 ret = -ENOTTY;
997 }
998
999 return ret;
1000} /* memory_ioctl */
1001
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001002static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +02001003{
1004 int ret;
1005
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001006 mutex_lock(&mtd_mutex);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001007 ret = mtdchar_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001008 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +02001009
1010 return ret;
1011}
1012
Kevin Cernekee97718542009-04-08 22:53:13 -07001013#ifdef CONFIG_COMPAT
1014
1015struct mtd_oob_buf32 {
1016 u_int32_t start;
1017 u_int32_t length;
1018 compat_caddr_t ptr; /* unsigned char* */
1019};
1020
1021#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1022#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1023
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001024static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001025 unsigned long arg)
1026{
1027 struct mtd_file_info *mfi = file->private_data;
1028 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +01001029 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001030 int ret = 0;
1031
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001032 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001033
1034 switch (cmd) {
1035 case MEMWRITEOOB32:
1036 {
1037 struct mtd_oob_buf32 buf;
1038 struct mtd_oob_buf32 __user *buf_user = argp;
1039
1040 if (copy_from_user(&buf, argp, sizeof(buf)))
1041 ret = -EFAULT;
1042 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001043 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001044 buf.length, compat_ptr(buf.ptr),
1045 &buf_user->length);
1046 break;
1047 }
1048
1049 case MEMREADOOB32:
1050 {
1051 struct mtd_oob_buf32 buf;
1052 struct mtd_oob_buf32 __user *buf_user = argp;
1053
1054 /* NOTE: writes return length to buf->start */
1055 if (copy_from_user(&buf, argp, sizeof(buf)))
1056 ret = -EFAULT;
1057 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001058 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001059 buf.length, compat_ptr(buf.ptr),
1060 &buf_user->start);
1061 break;
1062 }
1063 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001064 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001065 }
1066
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001067 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001068
1069 return ret;
1070}
1071
1072#endif /* CONFIG_COMPAT */
1073
David Howells402d3262009-02-12 10:40:00 +00001074/*
1075 * try to determine where a shared mapping can be made
1076 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1077 * mappings)
1078 */
1079#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001080static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001081 unsigned long addr,
1082 unsigned long len,
1083 unsigned long pgoff,
1084 unsigned long flags)
1085{
1086 struct mtd_file_info *mfi = file->private_data;
1087 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiycd621272011-12-30 14:31:57 +02001088 unsigned long offset;
1089 int ret;
David Howells402d3262009-02-12 10:40:00 +00001090
Artem Bityutskiycd621272011-12-30 14:31:57 +02001091 if (addr != 0)
1092 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001093
Artem Bityutskiycd621272011-12-30 14:31:57 +02001094 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1095 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001096
Artem Bityutskiycd621272011-12-30 14:31:57 +02001097 offset = pgoff << PAGE_SHIFT;
1098 if (offset > mtd->size - len)
1099 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001100
Artem Bityutskiycd621272011-12-30 14:31:57 +02001101 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
1102 return ret == -EOPNOTSUPP ? -ENOSYS : ret;
David Howells402d3262009-02-12 10:40:00 +00001103}
1104#endif
1105
1106/*
1107 * set up a mapping for shared memory segments
1108 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001109static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001110{
1111#ifdef CONFIG_MMU
1112 struct mtd_file_info *mfi = file->private_data;
1113 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001114 struct map_info *map = mtd->priv;
David Howells402d3262009-02-12 10:40:00 +00001115
David Woodhousef5cf8f02012-10-09 15:08:10 +01001116 /* This is broken because it assumes the MTD device is map-based
1117 and that mtd->priv is a valid struct map_info. It should be
1118 replaced with something that uses the mtd_get_unmapped_area()
1119 operation properly. */
1120 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001121#ifdef pgprot_noncached
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001122 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001123 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1124#endif
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001125 return vm_iomap_memory(vma, map->phys, map->size);
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001126 }
David Howells402d3262009-02-12 10:40:00 +00001127 return -ENOSYS;
1128#else
1129 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1130#endif
1131}
1132
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001133static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001135 .llseek = mtdchar_lseek,
1136 .read = mtdchar_read,
1137 .write = mtdchar_write,
1138 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001139#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001140 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001141#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001142 .open = mtdchar_open,
1143 .release = mtdchar_close,
1144 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001145#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001146 .get_unmapped_area = mtdchar_get_unmapped_area,
David Howells402d3262009-02-12 10:40:00 +00001147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148};
1149
Al Viro00292bb2012-03-23 13:47:52 -04001150static const struct super_operations mtd_ops = {
1151 .drop_inode = generic_delete_inode,
1152 .statfs = simple_statfs,
1153};
1154
Al Viro51139ad2010-07-25 23:47:46 +04001155static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1156 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001157{
Al Viro00292bb2012-03-23 13:47:52 -04001158 return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001159}
1160
1161static struct file_system_type mtd_inodefs_type = {
1162 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001163 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001164 .kill_sb = kill_anon_super,
1165};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001166MODULE_ALIAS_FS("mtd_inodefs");
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001167
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001168int __init init_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001170 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001171
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001172 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001173 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001174 if (ret < 0) {
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001175 pr_err("Can't allocate major number %d for MTD\n",
1176 MTD_CHAR_MAJOR);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001180 ret = register_filesystem(&mtd_inodefs_type);
1181 if (ret) {
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001182 pr_err("Can't register mtd_inodefs filesystem, error %d\n",
1183 ret);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001184 goto err_unregister_chdev;
1185 }
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001186
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001187 return ret;
1188
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001189err_unregister_chdev:
1190 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001194void __exit cleanup_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001196 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001197 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Scott James Remnant90160e12009-03-02 18:42:39 +00001200MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);