blob: 6afb05469bbd8db2735566a1c9c2e084cea19f2d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mtd/mtd.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030035#include <linux/mtd/partitions.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020036#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Gleixner15fdc522005-11-07 00:14:42 +010038#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010039
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030040#define MTD_INODE_FS_MAGIC 0x11307854
Arnd Bergmann5aa82942010-06-02 14:28:52 +020041static DEFINE_MUTEX(mtd_mutex);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030042static struct vfsmount *mtd_inode_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Nicolas Pitre045e9a52005-02-08 19:12:53 +000044/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020045 * Data structure to hold the pointer to the mtd device as well
Brian Norris92394b52011-07-20 09:53:42 -070046 * as mode information of various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000047 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020048struct mtd_file_info {
49 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030050 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020051 enum mtd_file_modes mode;
52};
Nicolas Pitre31f42332005-02-08 17:45:55 +000053
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020054static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020056 struct mtd_file_info *mfi = file->private_data;
57 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040060 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040062 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010063 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040065 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010066 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
68 default:
69 return -EINVAL;
70 }
71
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020072 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010073 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Todd Poynor8b491d72005-08-04 02:05:51 +010075 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78
79
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020080static int mtdchar_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 int minor = iminor(inode);
83 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060084 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020086 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030087 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Brian Norris289c0522011-07-19 10:06:09 -070089 pr_debug("MTD_open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040092 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -EACCES;
94
Arnd Bergmann5aa82942010-06-02 14:28:52 +020095 mutex_lock(&mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000097
Jonathan Corbet60712392008-05-15 10:10:37 -060098 if (IS_ERR(mtd)) {
99 ret = PTR_ERR(mtd);
100 goto out;
101 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000102
David Howells402d3262009-02-12 10:40:00 +0000103 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600105 ret = -ENODEV;
106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300109 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
110 if (!mtd_ino) {
111 put_mtd_device(mtd);
112 ret = -ENOMEM;
113 goto out;
114 }
115 if (mtd_ino->i_state & I_NEW) {
116 mtd_ino->i_private = mtd;
117 mtd_ino->i_mode = S_IFCHR;
118 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
119 unlock_new_inode(mtd_ino);
120 }
121 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400124 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300125 iput(mtd_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600127 ret = -EACCES;
128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000130
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200131 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
132 if (!mfi) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300133 iput(mtd_ino);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200134 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600135 ret = -ENOMEM;
136 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200137 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300138 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200139 mfi->mtd = mtd;
140 file->private_data = mfi;
141
Jonathan Corbet60712392008-05-15 10:10:37 -0600142out:
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200143 mutex_unlock(&mtd_mutex);
Jonathan Corbet60712392008-05-15 10:10:37 -0600144 return ret;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200145} /* mtdchar_open */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*====================================================================*/
148
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200149static int mtdchar_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200151 struct mtd_file_info *mfi = file->private_data;
152 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Brian Norris289c0522011-07-19 10:06:09 -0700154 pr_debug("MTD_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200156 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400157 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000159
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300160 iput(mfi->ino);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200163 file->private_data = NULL;
164 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return 0;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200167} /* mtdchar_close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Grant Erickson3e45cf52011-04-08 08:51:33 -0700169/* Back in June 2001, dwmw2 wrote:
170 *
171 * FIXME: This _really_ needs to die. In 2.5, we should lock the
172 * userspace buffer down and use it directly with readv/writev.
173 *
174 * The implementation below, using mtd_kmalloc_up_to, mitigates
175 * allocation failures when the system is under low-memory situations
176 * or if memory is highly fragmented at the cost of reducing the
177 * performance of the requested transfer due to a smaller buffer size.
178 *
179 * A more complex but more memory-efficient implementation based on
180 * get_user_pages and iovecs to cover extents of those pages is a
181 * longer-term goal, as intimated by dwmw2 above. However, for the
182 * write case, this requires yet more complex head and tail transfer
183 * handling when those head and tail offsets and sizes are such that
184 * alignment requirements are not met in the NAND subdriver.
185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200187static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
188 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200190 struct mtd_file_info *mfi = file->private_data;
191 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 size_t retlen=0;
193 size_t total_retlen=0;
194 int ret=0;
195 int len;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700196 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000198
Brian Norris289c0522011-07-19 10:06:09 -0700199 pr_debug("MTD_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (*ppos + count > mtd->size)
202 count = mtd->size - *ppos;
203
204 if (!count)
205 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000206
Grant Erickson3e45cf52011-04-08 08:51:33 -0700207 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100208 if (!kbuf)
209 return -ENOMEM;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700212 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200214 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700215 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200216 ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
217 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000218 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700219 case MTD_FILE_MODE_OTP_USER:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000220 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
221 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700222 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200223 {
224 struct mtd_oob_ops ops;
225
Brian Norris0612b9d2011-08-30 18:45:40 -0700226 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200227 ops.datbuf = kbuf;
228 ops.oobbuf = NULL;
229 ops.len = len;
230
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200231 ret = mtd_read_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200232 retlen = ops.retlen;
233 break;
234 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000235 default:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200236 ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000237 }
Brian Norris7854d3f2011-06-23 14:12:08 -0700238 /* Nand returns -EBADMSG on ECC errors, but it returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * the data. For our userspace tools it is important
Brian Norris7854d3f2011-06-23 14:12:08 -0700240 * to dump areas with ECC errors!
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200241 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300242 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200243 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * Userspace software which accesses NAND this way
245 * must be aware of the fact that it deals with NAND
246 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700247 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *ppos += retlen;
249 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200250 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EFAULT;
252 }
253 else
254 total_retlen += retlen;
255
256 count -= retlen;
257 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000258 if (retlen == 0)
259 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 else {
262 kfree(kbuf);
263 return ret;
264 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Thago Galesib802c072006-04-17 17:38:15 +0100268 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200270} /* mtdchar_read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200272static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
273 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200275 struct mtd_file_info *mfi = file->private_data;
276 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700277 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 char *kbuf;
279 size_t retlen;
280 size_t total_retlen=0;
281 int ret=0;
282 int len;
283
Brian Norris289c0522011-07-19 10:06:09 -0700284 pr_debug("MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (*ppos == mtd->size)
287 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (*ppos + count > mtd->size)
290 count = mtd->size - *ppos;
291
292 if (!count)
293 return 0;
294
Grant Erickson3e45cf52011-04-08 08:51:33 -0700295 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100296 if (!kbuf)
297 return -ENOMEM;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700300 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (copy_from_user(kbuf, buf, len)) {
303 kfree(kbuf);
304 return -EFAULT;
305 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000306
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200307 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700308 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000309 ret = -EROFS;
310 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700311 case MTD_FILE_MODE_OTP_USER:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000312 if (!mtd->write_user_prot_reg) {
313 ret = -EOPNOTSUPP;
314 break;
315 }
316 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
317 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200318
Brian Norrisbeb133f2011-08-30 18:45:41 -0700319 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200320 {
321 struct mtd_oob_ops ops;
322
Brian Norris0612b9d2011-08-30 18:45:40 -0700323 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200324 ops.datbuf = kbuf;
325 ops.oobbuf = NULL;
Peter Wippichbf514082011-06-06 15:50:58 +0200326 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200327 ops.len = len;
328
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200329 ret = mtd_write_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200330 retlen = ops.retlen;
331 break;
332 }
333
Nicolas Pitre31f42332005-02-08 17:45:55 +0000334 default:
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200335 ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!ret) {
338 *ppos += retlen;
339 total_retlen += retlen;
340 count -= retlen;
341 buf += retlen;
342 }
343 else {
344 kfree(kbuf);
345 return ret;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Thago Galesib802c072006-04-17 17:38:15 +0100349 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200351} /* mtdchar_write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/*======================================================================
354
355 IOCTL calls for getting device parameters.
356
357======================================================================*/
358static void mtdchar_erase_callback (struct erase_info *instr)
359{
360 wake_up((wait_queue_head_t *)instr->priv);
361}
362
David Brownell34a82442008-07-30 12:35:05 -0700363#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200364static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
365{
366 struct mtd_info *mtd = mfi->mtd;
367 int ret = 0;
368
369 switch (mode) {
370 case MTD_OTP_FACTORY:
371 if (!mtd->read_fact_prot_reg)
372 ret = -EOPNOTSUPP;
373 else
Brian Norrisbeb133f2011-08-30 18:45:41 -0700374 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200375 break;
376 case MTD_OTP_USER:
377 if (!mtd->read_fact_prot_reg)
378 ret = -EOPNOTSUPP;
379 else
Brian Norrisbeb133f2011-08-30 18:45:41 -0700380 mfi->mode = MTD_FILE_MODE_OTP_USER;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200381 break;
382 default:
383 ret = -EINVAL;
384 case MTD_OTP_OFF:
385 break;
386 }
387 return ret;
388}
389#else
390# define otp_select_filemode(f,m) -EOPNOTSUPP
391#endif
392
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200393static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
Kevin Cernekee97718542009-04-08 22:53:13 -0700394 uint64_t start, uint32_t length, void __user *ptr,
395 uint32_t __user *retp)
396{
Brian Norris9ce244b2011-08-30 18:45:37 -0700397 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700398 struct mtd_oob_ops ops;
399 uint32_t retlen;
400 int ret = 0;
401
402 if (!(file->f_mode & FMODE_WRITE))
403 return -EPERM;
404
405 if (length > 4096)
406 return -EINVAL;
407
408 if (!mtd->write_oob)
409 ret = -EOPNOTSUPP;
410 else
Roel Kluin00404762010-01-29 10:35:04 +0100411 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700412
413 if (ret)
414 return ret;
415
416 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700417 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700418 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700419 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700420 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700421
422 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
423 return -EINVAL;
424
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200425 ops.oobbuf = memdup_user(ptr, length);
426 if (IS_ERR(ops.oobbuf))
427 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700428
Brian Norris305b93f2011-08-23 17:17:32 -0700429 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200430 ret = mtd_write_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700431
432 if (ops.oobretlen > 0xFFFFFFFFU)
433 ret = -EOVERFLOW;
434 retlen = ops.oobretlen;
435 if (copy_to_user(retp, &retlen, sizeof(length)))
436 ret = -EFAULT;
437
438 kfree(ops.oobbuf);
439 return ret;
440}
441
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200442static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
Brian Norrisc46f6482011-08-30 18:45:38 -0700443 uint64_t start, uint32_t length, void __user *ptr,
444 uint32_t __user *retp)
Kevin Cernekee97718542009-04-08 22:53:13 -0700445{
Brian Norrisc46f6482011-08-30 18:45:38 -0700446 struct mtd_file_info *mfi = file->private_data;
Kevin Cernekee97718542009-04-08 22:53:13 -0700447 struct mtd_oob_ops ops;
448 int ret = 0;
449
450 if (length > 4096)
451 return -EINVAL;
452
453 if (!mtd->read_oob)
454 ret = -EOPNOTSUPP;
455 else
456 ret = access_ok(VERIFY_WRITE, ptr,
457 length) ? 0 : -EFAULT;
458 if (ret)
459 return ret;
460
461 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700462 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700463 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700464 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700465 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700466
467 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
468 return -EINVAL;
469
470 ops.oobbuf = kmalloc(length, GFP_KERNEL);
471 if (!ops.oobbuf)
472 return -ENOMEM;
473
Brian Norris305b93f2011-08-23 17:17:32 -0700474 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200475 ret = mtd_read_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700476
477 if (put_user(ops.oobretlen, retp))
478 ret = -EFAULT;
479 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
480 ops.oobretlen))
481 ret = -EFAULT;
482
483 kfree(ops.oobbuf);
Brian Norris041e4572011-06-23 16:45:24 -0700484
485 /*
486 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
487 * data. For our userspace tools it is important to dump areas
488 * with ECC errors!
489 * For kernel internal usage it also might return -EUCLEAN
490 * to signal the caller that a bitflip has occured and has
491 * been corrected by the ECC algorithm.
492 *
Brian Norrisc478d7e2011-06-28 16:29:00 -0700493 * Note: currently the standard NAND function, nand_read_oob_std,
494 * does not calculate ECC for the OOB area, so do not rely on
495 * this behavior unless you have replaced it with your own.
Brian Norris041e4572011-06-23 16:45:24 -0700496 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700497 if (mtd_is_bitflip_or_eccerr(ret))
Brian Norris041e4572011-06-23 16:45:24 -0700498 return 0;
499
Kevin Cernekee97718542009-04-08 22:53:13 -0700500 return ret;
501}
502
Brian Norriscc26c3c2010-08-24 18:12:00 -0700503/*
504 * Copies (and truncates, if necessary) data from the larger struct,
505 * nand_ecclayout, to the smaller, deprecated layout struct,
Brian Norris92394b52011-07-20 09:53:42 -0700506 * nand_ecclayout_user. This is necessary only to support the deprecated
Brian Norriscc26c3c2010-08-24 18:12:00 -0700507 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
508 * nand_ecclayout flexibly (i.e. the struct may change size in new
509 * releases without requiring major rewrites).
510 */
511static int shrink_ecclayout(const struct nand_ecclayout *from,
512 struct nand_ecclayout_user *to)
513{
514 int i;
515
516 if (!from || !to)
517 return -EINVAL;
518
519 memset(to, 0, sizeof(*to));
520
Brian Norris0ceacf32010-09-19 23:57:12 -0700521 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700522 for (i = 0; i < to->eccbytes; i++)
523 to->eccpos[i] = from->eccpos[i];
524
525 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
526 if (from->oobfree[i].length == 0 &&
527 from->oobfree[i].offset == 0)
528 break;
529 to->oobavail += from->oobfree[i].length;
530 to->oobfree[i] = from->oobfree[i];
531 }
532
533 return 0;
534}
535
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200536static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300537 struct blkpg_ioctl_arg __user *arg)
538{
539 struct blkpg_ioctl_arg a;
540 struct blkpg_partition p;
541
542 if (!capable(CAP_SYS_ADMIN))
543 return -EPERM;
544
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300545 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
546 return -EFAULT;
547
548 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
549 return -EFAULT;
550
551 switch (a.op) {
552 case BLKPG_ADD_PARTITION:
553
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200554 /* Only master mtd device must be used to add partitions */
555 if (mtd_is_partition(mtd))
556 return -EINVAL;
557
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300558 return mtd_add_partition(mtd, p.devname, p.start, p.length);
559
560 case BLKPG_DEL_PARTITION:
561
562 if (p.pno < 0)
563 return -EINVAL;
564
565 return mtd_del_partition(mtd, p.pno);
566
567 default:
568 return -EINVAL;
569 }
570}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300571
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200572static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700573 struct mtd_write_req __user *argp)
574{
575 struct mtd_write_req req;
576 struct mtd_oob_ops ops;
577 void __user *usr_data, *usr_oob;
578 int ret;
579
580 if (copy_from_user(&req, argp, sizeof(req)) ||
581 !access_ok(VERIFY_READ, req.usr_data, req.len) ||
582 !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
583 return -EFAULT;
584 if (!mtd->write_oob)
585 return -EOPNOTSUPP;
586
587 ops.mode = req.mode;
588 ops.len = (size_t)req.len;
589 ops.ooblen = (size_t)req.ooblen;
590 ops.ooboffs = 0;
591
592 usr_data = (void __user *)(uintptr_t)req.usr_data;
593 usr_oob = (void __user *)(uintptr_t)req.usr_oob;
594
595 if (req.usr_data) {
596 ops.datbuf = memdup_user(usr_data, ops.len);
597 if (IS_ERR(ops.datbuf))
598 return PTR_ERR(ops.datbuf);
599 } else {
600 ops.datbuf = NULL;
601 }
602
603 if (req.usr_oob) {
604 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
605 if (IS_ERR(ops.oobbuf)) {
606 kfree(ops.datbuf);
607 return PTR_ERR(ops.oobbuf);
608 }
609 } else {
610 ops.oobbuf = NULL;
611 }
612
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200613 ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
Brian Norrise99d8b02011-09-09 09:59:03 -0700614
615 kfree(ops.datbuf);
616 kfree(ops.oobbuf);
617
618 return ret;
619}
620
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200621static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200623 struct mtd_file_info *mfi = file->private_data;
624 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 void __user *argp = (void __user *)arg;
626 int ret = 0;
627 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200628 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000629
Brian Norris289c0522011-07-19 10:06:09 -0700630 pr_debug("MTD_ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
633 if (cmd & IOC_IN) {
634 if (!access_ok(VERIFY_READ, argp, size))
635 return -EFAULT;
636 }
637 if (cmd & IOC_OUT) {
638 if (!access_ok(VERIFY_WRITE, argp, size))
639 return -EFAULT;
640 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 switch (cmd) {
643 case MEMGETREGIONCOUNT:
644 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
645 return -EFAULT;
646 break;
647
648 case MEMGETREGIONINFO:
649 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700650 uint32_t ur_idx;
651 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700652 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Zev Weissb67c5f82008-09-01 05:02:12 -0700654 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -EFAULT;
656
Dan Carpenter5e59be12010-09-08 21:39:56 +0200657 if (ur_idx >= mtd->numeraseregions)
658 return -EINVAL;
659
Zev Weissb67c5f82008-09-01 05:02:12 -0700660 kr = &(mtd->eraseregions[ur_idx]);
661
662 if (put_user(kr->offset, &(ur->offset))
663 || put_user(kr->erasesize, &(ur->erasesize))
664 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
668 }
669
670 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300671 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200672 info.type = mtd->type;
673 info.flags = mtd->flags;
674 info.size = mtd->size;
675 info.erasesize = mtd->erasesize;
676 info.writesize = mtd->writesize;
677 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700678 /* The below field is obsolete */
679 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200680 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -EFAULT;
682 break;
683
684 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700685 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 {
687 struct erase_info *erase;
688
Al Viroaeb5d722008-09-02 15:28:45 -0400689 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EPERM;
691
Burman Yan95b93a02006-11-15 21:10:29 +0200692 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!erase)
694 ret = -ENOMEM;
695 else {
696 wait_queue_head_t waitq;
697 DECLARE_WAITQUEUE(wait, current);
698
699 init_waitqueue_head(&waitq);
700
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700701 if (cmd == MEMERASE64) {
702 struct erase_info_user64 einfo64;
703
704 if (copy_from_user(&einfo64, argp,
705 sizeof(struct erase_info_user64))) {
706 kfree(erase);
707 return -EFAULT;
708 }
709 erase->addr = einfo64.start;
710 erase->len = einfo64.length;
711 } else {
712 struct erase_info_user einfo32;
713
714 if (copy_from_user(&einfo32, argp,
715 sizeof(struct erase_info_user))) {
716 kfree(erase);
717 return -EFAULT;
718 }
719 erase->addr = einfo32.start;
720 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722 erase->mtd = mtd;
723 erase->callback = mtdchar_erase_callback;
724 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 FIXME: Allow INTERRUPTIBLE. Which means
728 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 If the wq_head is on the stack, and we
731 leave because we got interrupted, then the
732 wq_head is no longer there when the
733 callback routine tries to wake us up.
734 */
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200735 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!ret) {
737 set_current_state(TASK_UNINTERRUPTIBLE);
738 add_wait_queue(&waitq, &wait);
739 if (erase->state != MTD_ERASE_DONE &&
740 erase->state != MTD_ERASE_FAILED)
741 schedule();
742 remove_wait_queue(&waitq, &wait);
743 set_current_state(TASK_RUNNING);
744
745 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
746 }
747 kfree(erase);
748 }
749 break;
750 }
751
752 case MEMWRITEOOB:
753 {
754 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700755 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000756
Kevin Cernekee97718542009-04-08 22:53:13 -0700757 /* NOTE: writes return length to buf_user->length */
758 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700760 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200761 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700762 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 case MEMREADOOB:
767 {
768 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700769 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Kevin Cernekee97718542009-04-08 22:53:13 -0700771 /* NOTE: writes return length to buf_user->start */
772 if (copy_from_user(&buf, argp, sizeof(buf)))
773 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200775 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700776 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778 }
779
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700780 case MEMWRITEOOB64:
781 {
782 struct mtd_oob_buf64 buf;
783 struct mtd_oob_buf64 __user *buf_user = argp;
784
785 if (copy_from_user(&buf, argp, sizeof(buf)))
786 ret = -EFAULT;
787 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200788 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700789 (void __user *)(uintptr_t)buf.usr_ptr,
790 &buf_user->length);
791 break;
792 }
793
794 case MEMREADOOB64:
795 {
796 struct mtd_oob_buf64 buf;
797 struct mtd_oob_buf64 __user *buf_user = argp;
798
799 if (copy_from_user(&buf, argp, sizeof(buf)))
800 ret = -EFAULT;
801 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200802 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700803 (void __user *)(uintptr_t)buf.usr_ptr,
804 &buf_user->length);
805 break;
806 }
807
Brian Norrise99d8b02011-09-09 09:59:03 -0700808 case MEMWRITE:
809 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200810 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700811 (struct mtd_write_req __user *)arg);
812 break;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 case MEMLOCK:
816 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700817 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Harvey Harrison175428b2008-07-03 23:40:14 -0700819 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EFAULT;
821
822 if (!mtd->lock)
823 ret = -EOPNOTSUPP;
824 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700825 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827 }
828
829 case MEMUNLOCK:
830 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700831 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Harvey Harrison175428b2008-07-03 23:40:14 -0700833 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return -EFAULT;
835
836 if (!mtd->unlock)
837 ret = -EOPNOTSUPP;
838 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700839 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
841 }
842
Richard Cochran99384242010-06-14 18:10:33 +0200843 case MEMISLOCKED:
844 {
845 struct erase_info_user einfo;
846
847 if (copy_from_user(&einfo, argp, sizeof(einfo)))
848 return -EFAULT;
849
850 if (!mtd->is_locked)
851 ret = -EOPNOTSUPP;
852 else
853 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
854 break;
855 }
856
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200857 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case MEMGETOOBSEL:
859 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200860 struct nand_oobinfo oi;
861
862 if (!mtd->ecclayout)
863 return -EOPNOTSUPP;
864 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
865 return -EINVAL;
866
867 oi.useecc = MTD_NANDECC_AUTOPLACE;
868 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
869 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
870 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200871 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200872
873 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return -EFAULT;
875 break;
876 }
877
878 case MEMGETBADBLOCK:
879 {
880 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (copy_from_user(&offs, argp, sizeof(loff_t)))
883 return -EFAULT;
884 if (!mtd->block_isbad)
885 ret = -EOPNOTSUPP;
886 else
887 return mtd->block_isbad(mtd, offs);
888 break;
889 }
890
891 case MEMSETBADBLOCK:
892 {
893 loff_t offs;
894
895 if (copy_from_user(&offs, argp, sizeof(loff_t)))
896 return -EFAULT;
897 if (!mtd->block_markbad)
898 ret = -EOPNOTSUPP;
899 else
900 return mtd->block_markbad(mtd, offs);
901 break;
902 }
903
David Brownell34a82442008-07-30 12:35:05 -0700904#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000905 case OTPSELECT:
906 {
907 int mode;
908 if (copy_from_user(&mode, argp, sizeof(int)))
909 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200910
Brian Norrisbeb133f2011-08-30 18:45:41 -0700911 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200912
913 ret = otp_select_filemode(mfi, mode);
914
Nicolas Pitre81dba482005-04-01 16:36:15 +0100915 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000916 break;
917 }
918
919 case OTPGETREGIONCOUNT:
920 case OTPGETREGIONINFO:
921 {
922 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
923 if (!buf)
924 return -ENOMEM;
925 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200926 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700927 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000928 if (mtd->get_fact_prot_info)
Artem Bityutskiya750b5c2011-12-23 18:33:28 +0200929 ret = mtd_get_fact_prot_info(mtd, buf, 4096);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000930 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700931 case MTD_FILE_MODE_OTP_USER:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000932 if (mtd->get_user_prot_info)
933 ret = mtd->get_user_prot_info(mtd, buf, 4096);
934 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200935 default:
936 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000937 }
938 if (ret >= 0) {
939 if (cmd == OTPGETREGIONCOUNT) {
940 int nbr = ret / sizeof(struct otp_info);
941 ret = copy_to_user(argp, &nbr, sizeof(int));
942 } else
943 ret = copy_to_user(argp, buf, ret);
944 if (ret)
945 ret = -EFAULT;
946 }
947 kfree(buf);
948 break;
949 }
950
951 case OTPLOCK:
952 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700953 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000954
Brian Norrisbeb133f2011-08-30 18:45:41 -0700955 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000956 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700957 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000958 return -EFAULT;
959 if (!mtd->lock_user_prot_reg)
960 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700961 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000962 break;
963 }
964#endif
965
Brian Norris7854d3f2011-06-23 14:12:08 -0700966 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200967 case ECCGETLAYOUT:
968 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700969 struct nand_ecclayout_user *usrlay;
970
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200971 if (!mtd->ecclayout)
972 return -EOPNOTSUPP;
973
Brian Norriscc26c3c2010-08-24 18:12:00 -0700974 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
975 if (!usrlay)
976 return -ENOMEM;
977
978 shrink_ecclayout(mtd->ecclayout, usrlay);
979
980 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
981 ret = -EFAULT;
982 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200983 break;
984 }
985
986 case ECCGETSTATS:
987 {
988 if (copy_to_user(argp, &mtd->ecc_stats,
989 sizeof(struct mtd_ecc_stats)))
990 return -EFAULT;
991 break;
992 }
993
994 case MTDFILEMODE:
995 {
996 mfi->mode = 0;
997
998 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700999 case MTD_FILE_MODE_OTP_FACTORY:
1000 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001001 ret = otp_select_filemode(mfi, arg);
1002 break;
1003
Brian Norrisbeb133f2011-08-30 18:45:41 -07001004 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001005 if (!mtd->read_oob || !mtd->write_oob)
1006 return -EOPNOTSUPP;
1007 mfi->mode = arg;
1008
Brian Norrisbeb133f2011-08-30 18:45:41 -07001009 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +02001010 break;
1011 default:
1012 ret = -EINVAL;
1013 }
1014 file->f_pos = 0;
1015 break;
1016 }
1017
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001018 case BLKPG:
1019 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001020 ret = mtdchar_blkpg_ioctl(mtd,
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001021 (struct blkpg_ioctl_arg __user *)arg);
1022 break;
1023 }
1024
1025 case BLKRRPART:
1026 {
1027 /* No reread partition feature. Just return ok */
1028 ret = 0;
1029 break;
1030 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +03001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 default:
1033 ret = -ENOTTY;
1034 }
1035
1036 return ret;
1037} /* memory_ioctl */
1038
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001039static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +02001040{
1041 int ret;
1042
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001043 mutex_lock(&mtd_mutex);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001044 ret = mtdchar_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001045 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +02001046
1047 return ret;
1048}
1049
Kevin Cernekee97718542009-04-08 22:53:13 -07001050#ifdef CONFIG_COMPAT
1051
1052struct mtd_oob_buf32 {
1053 u_int32_t start;
1054 u_int32_t length;
1055 compat_caddr_t ptr; /* unsigned char* */
1056};
1057
1058#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1059#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1060
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001061static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001062 unsigned long arg)
1063{
1064 struct mtd_file_info *mfi = file->private_data;
1065 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +01001066 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001067 int ret = 0;
1068
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001069 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001070
1071 switch (cmd) {
1072 case MEMWRITEOOB32:
1073 {
1074 struct mtd_oob_buf32 buf;
1075 struct mtd_oob_buf32 __user *buf_user = argp;
1076
1077 if (copy_from_user(&buf, argp, sizeof(buf)))
1078 ret = -EFAULT;
1079 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001080 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001081 buf.length, compat_ptr(buf.ptr),
1082 &buf_user->length);
1083 break;
1084 }
1085
1086 case MEMREADOOB32:
1087 {
1088 struct mtd_oob_buf32 buf;
1089 struct mtd_oob_buf32 __user *buf_user = argp;
1090
1091 /* NOTE: writes return length to buf->start */
1092 if (copy_from_user(&buf, argp, sizeof(buf)))
1093 ret = -EFAULT;
1094 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001095 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001096 buf.length, compat_ptr(buf.ptr),
1097 &buf_user->start);
1098 break;
1099 }
1100 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001101 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001102 }
1103
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001104 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001105
1106 return ret;
1107}
1108
1109#endif /* CONFIG_COMPAT */
1110
David Howells402d3262009-02-12 10:40:00 +00001111/*
1112 * try to determine where a shared mapping can be made
1113 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1114 * mappings)
1115 */
1116#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001117static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001118 unsigned long addr,
1119 unsigned long len,
1120 unsigned long pgoff,
1121 unsigned long flags)
1122{
1123 struct mtd_file_info *mfi = file->private_data;
1124 struct mtd_info *mtd = mfi->mtd;
1125
1126 if (mtd->get_unmapped_area) {
1127 unsigned long offset;
1128
1129 if (addr != 0)
1130 return (unsigned long) -EINVAL;
1131
1132 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1133 return (unsigned long) -EINVAL;
1134
1135 offset = pgoff << PAGE_SHIFT;
1136 if (offset > mtd->size - len)
1137 return (unsigned long) -EINVAL;
1138
Artem Bityutskiy04c601b2011-12-23 17:10:15 +02001139 return mtd_get_unmapped_area(mtd, len, offset, flags);
David Howells402d3262009-02-12 10:40:00 +00001140 }
1141
1142 /* can't map directly */
1143 return (unsigned long) -ENOSYS;
1144}
1145#endif
1146
1147/*
1148 * set up a mapping for shared memory segments
1149 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001150static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001151{
1152#ifdef CONFIG_MMU
1153 struct mtd_file_info *mfi = file->private_data;
1154 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001155 struct map_info *map = mtd->priv;
1156 unsigned long start;
1157 unsigned long off;
1158 u32 len;
David Howells402d3262009-02-12 10:40:00 +00001159
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001160 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1161 off = vma->vm_pgoff << PAGE_SHIFT;
1162 start = map->phys;
1163 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1164 start &= PAGE_MASK;
1165 if ((vma->vm_end - vma->vm_start + off) > len)
1166 return -EINVAL;
1167
1168 off += start;
1169 vma->vm_pgoff = off >> PAGE_SHIFT;
1170 vma->vm_flags |= VM_IO | VM_RESERVED;
1171
1172#ifdef pgprot_noncached
1173 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1174 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1175#endif
1176 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1177 vma->vm_end - vma->vm_start,
1178 vma->vm_page_prot))
1179 return -EAGAIN;
1180
David Howells402d3262009-02-12 10:40:00 +00001181 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001182 }
David Howells402d3262009-02-12 10:40:00 +00001183 return -ENOSYS;
1184#else
1185 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1186#endif
1187}
1188
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001189static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001191 .llseek = mtdchar_lseek,
1192 .read = mtdchar_read,
1193 .write = mtdchar_write,
1194 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001195#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001196 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001197#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001198 .open = mtdchar_open,
1199 .release = mtdchar_close,
1200 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001201#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001202 .get_unmapped_area = mtdchar_get_unmapped_area,
David Howells402d3262009-02-12 10:40:00 +00001203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204};
1205
Al Viro51139ad2010-07-25 23:47:46 +04001206static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1207 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001208{
Al Viroc74a1cb2011-01-12 16:59:34 -05001209 return mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001210}
1211
1212static struct file_system_type mtd_inodefs_type = {
1213 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001214 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001215 .kill_sb = kill_anon_super,
1216};
1217
1218static void mtdchar_notify_add(struct mtd_info *mtd)
1219{
1220}
1221
1222static void mtdchar_notify_remove(struct mtd_info *mtd)
1223{
1224 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1225
1226 if (mtd_ino) {
1227 /* Destroy the inode if it exists */
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02001228 clear_nlink(mtd_ino);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001229 iput(mtd_ino);
1230 }
1231}
1232
1233static struct mtd_notifier mtdchar_notifier = {
1234 .add = mtdchar_notify_add,
1235 .remove = mtdchar_notify_remove,
1236};
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int __init init_mtdchar(void)
1239{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001240 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001241
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001242 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001243 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001244 if (ret < 0) {
1245 pr_notice("Can't allocate major number %d for "
1246 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001250 ret = register_filesystem(&mtd_inodefs_type);
1251 if (ret) {
1252 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1253 goto err_unregister_chdev;
1254 }
1255
1256 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1257 if (IS_ERR(mtd_inode_mnt)) {
1258 ret = PTR_ERR(mtd_inode_mnt);
1259 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1260 goto err_unregister_filesystem;
1261 }
1262 register_mtd_user(&mtdchar_notifier);
1263
1264 return ret;
1265
1266err_unregister_filesystem:
1267 unregister_filesystem(&mtd_inodefs_type);
1268err_unregister_chdev:
1269 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
1273static void __exit cleanup_mtdchar(void)
1274{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001275 unregister_mtd_user(&mtdchar_notifier);
Tim Chen423e0ab2011-07-19 09:32:38 -07001276 kern_unmount(mtd_inode_mnt);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001277 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001278 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281module_init(init_mtdchar);
1282module_exit(cleanup_mtdchar);
1283
David Brownell1f24b5a2009-03-26 00:42:41 -07001284MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286MODULE_LICENSE("GPL");
1287MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1288MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001289MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);