blob: f511dd15fd318b3a83c5c270fd2344845a648fcc [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
46 * as mode information ofr 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
55{
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
80static int mtd_open(struct inode *inode, struct file *file)
81{
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
89 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
90
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145} /* mtd_open */
146
147/*====================================================================*/
148
149static int mtd_close(struct inode *inode, struct file *file)
150{
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
154 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
155
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;
167} /* mtd_close */
168
169/* FIXME: This _really_ needs to die. In 2.5, we should lock the
170 userspace buffer down and use it directly with readv/writev.
171*/
172#define MAX_KMALLOC_SIZE 0x20000
173
174static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
175{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200176 struct mtd_file_info *mfi = file->private_data;
177 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 size_t retlen=0;
179 size_t total_retlen=0;
180 int ret=0;
181 int len;
182 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
185
186 if (*ppos + count > mtd->size)
187 count = mtd->size - *ppos;
188
189 if (!count)
190 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
193 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100194
195 if (count > MAX_KMALLOC_SIZE)
196 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
197 else
198 kbuf=kmalloc(count, GFP_KERNEL);
199
200 if (!kbuf)
201 return -ENOMEM;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100204
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000205 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 len = MAX_KMALLOC_SIZE;
207 else
208 len = count;
209
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200210 switch (mfi->mode) {
211 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000212 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
213 break;
214 case MTD_MODE_OTP_USER:
215 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
216 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200217 case MTD_MODE_RAW:
218 {
219 struct mtd_oob_ops ops;
220
221 ops.mode = MTD_OOB_RAW;
222 ops.datbuf = kbuf;
223 ops.oobbuf = NULL;
224 ops.len = len;
225
226 ret = mtd->read_oob(mtd, *ppos, &ops);
227 retlen = ops.retlen;
228 break;
229 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000230 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200231 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Nand returns -EBADMSG on ecc errors, but it returns
234 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000235 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200236 * For kernel internal usage it also might return -EUCLEAN
237 * to signal the caller that a bitflip has occured and has
238 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * Userspace software which accesses NAND this way
240 * must be aware of the fact that it deals with NAND
241 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200242 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *ppos += retlen;
244 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200245 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -EFAULT;
247 }
248 else
249 total_retlen += retlen;
250
251 count -= retlen;
252 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000253 if (retlen == 0)
254 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 else {
257 kfree(kbuf);
258 return ret;
259 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
Thago Galesib802c072006-04-17 17:38:15 +0100263 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return total_retlen;
265} /* mtd_read */
266
267static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
268{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200269 struct mtd_file_info *mfi = file->private_data;
270 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 char *kbuf;
272 size_t retlen;
273 size_t total_retlen=0;
274 int ret=0;
275 int len;
276
277 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (*ppos == mtd->size)
280 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (*ppos + count > mtd->size)
283 count = mtd->size - *ppos;
284
285 if (!count)
286 return 0;
287
Thago Galesib802c072006-04-17 17:38:15 +0100288 if (count > MAX_KMALLOC_SIZE)
289 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
290 else
291 kbuf=kmalloc(count, GFP_KERNEL);
292
293 if (!kbuf)
294 return -ENOMEM;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100297
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000298 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 len = MAX_KMALLOC_SIZE;
300 else
301 len = count;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (copy_from_user(kbuf, buf, len)) {
304 kfree(kbuf);
305 return -EFAULT;
306 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000307
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200308 switch (mfi->mode) {
309 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000310 ret = -EROFS;
311 break;
312 case MTD_MODE_OTP_USER:
313 if (!mtd->write_user_prot_reg) {
314 ret = -EOPNOTSUPP;
315 break;
316 }
317 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
318 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200319
320 case MTD_MODE_RAW:
321 {
322 struct mtd_oob_ops ops;
323
324 ops.mode = MTD_OOB_RAW;
325 ops.datbuf = kbuf;
326 ops.oobbuf = NULL;
327 ops.len = len;
328
329 ret = mtd->write_oob(mtd, *ppos, &ops);
330 retlen = ops.retlen;
331 break;
332 }
333
Nicolas Pitre31f42332005-02-08 17:45:55 +0000334 default:
335 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
336 }
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;
351} /* mtd_write */
352
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
374 mfi->mode = MTD_MODE_OTP_FACTORY;
375 break;
376 case MTD_OTP_USER:
377 if (!mtd->read_fact_prot_reg)
378 ret = -EOPNOTSUPP;
379 else
380 mfi->mode = MTD_MODE_OTP_USER;
381 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
Kevin Cernekee97718542009-04-08 22:53:13 -0700393static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
394 uint64_t start, uint32_t length, void __user *ptr,
395 uint32_t __user *retp)
396{
397 struct mtd_oob_ops ops;
398 uint32_t retlen;
399 int ret = 0;
400
401 if (!(file->f_mode & FMODE_WRITE))
402 return -EPERM;
403
404 if (length > 4096)
405 return -EINVAL;
406
407 if (!mtd->write_oob)
408 ret = -EOPNOTSUPP;
409 else
Roel Kluin00404762010-01-29 10:35:04 +0100410 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700411
412 if (ret)
413 return ret;
414
415 ops.ooblen = length;
416 ops.ooboffs = start & (mtd->oobsize - 1);
417 ops.datbuf = NULL;
418 ops.mode = MTD_OOB_PLACE;
419
420 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
421 return -EINVAL;
422
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200423 ops.oobbuf = memdup_user(ptr, length);
424 if (IS_ERR(ops.oobbuf))
425 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700426
427 start &= ~((uint64_t)mtd->oobsize - 1);
428 ret = mtd->write_oob(mtd, start, &ops);
429
430 if (ops.oobretlen > 0xFFFFFFFFU)
431 ret = -EOVERFLOW;
432 retlen = ops.oobretlen;
433 if (copy_to_user(retp, &retlen, sizeof(length)))
434 ret = -EFAULT;
435
436 kfree(ops.oobbuf);
437 return ret;
438}
439
440static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
441 uint32_t length, void __user *ptr, uint32_t __user *retp)
442{
443 struct mtd_oob_ops ops;
444 int ret = 0;
445
446 if (length > 4096)
447 return -EINVAL;
448
449 if (!mtd->read_oob)
450 ret = -EOPNOTSUPP;
451 else
452 ret = access_ok(VERIFY_WRITE, ptr,
453 length) ? 0 : -EFAULT;
454 if (ret)
455 return ret;
456
457 ops.ooblen = length;
458 ops.ooboffs = start & (mtd->oobsize - 1);
459 ops.datbuf = NULL;
460 ops.mode = MTD_OOB_PLACE;
461
462 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
463 return -EINVAL;
464
465 ops.oobbuf = kmalloc(length, GFP_KERNEL);
466 if (!ops.oobbuf)
467 return -ENOMEM;
468
469 start &= ~((uint64_t)mtd->oobsize - 1);
470 ret = mtd->read_oob(mtd, start, &ops);
471
472 if (put_user(ops.oobretlen, retp))
473 ret = -EFAULT;
474 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
475 ops.oobretlen))
476 ret = -EFAULT;
477
478 kfree(ops.oobbuf);
479 return ret;
480}
481
Brian Norriscc26c3c2010-08-24 18:12:00 -0700482/*
483 * Copies (and truncates, if necessary) data from the larger struct,
484 * nand_ecclayout, to the smaller, deprecated layout struct,
485 * nand_ecclayout_user. This is necessary only to suppport the deprecated
486 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
487 * nand_ecclayout flexibly (i.e. the struct may change size in new
488 * releases without requiring major rewrites).
489 */
490static int shrink_ecclayout(const struct nand_ecclayout *from,
491 struct nand_ecclayout_user *to)
492{
493 int i;
494
495 if (!from || !to)
496 return -EINVAL;
497
498 memset(to, 0, sizeof(*to));
499
Brian Norris0ceacf32010-09-19 23:57:12 -0700500 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700501 for (i = 0; i < to->eccbytes; i++)
502 to->eccpos[i] = from->eccpos[i];
503
504 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
505 if (from->oobfree[i].length == 0 &&
506 from->oobfree[i].offset == 0)
507 break;
508 to->oobavail += from->oobfree[i].length;
509 to->oobfree[i] = from->oobfree[i];
510 }
511
512 return 0;
513}
514
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300515#ifdef CONFIG_MTD_PARTITIONS
516static int mtd_blkpg_ioctl(struct mtd_info *mtd,
517 struct blkpg_ioctl_arg __user *arg)
518{
519 struct blkpg_ioctl_arg a;
520 struct blkpg_partition p;
521
522 if (!capable(CAP_SYS_ADMIN))
523 return -EPERM;
524
525 /* Only master mtd device must be used to control partitions */
526 if (!mtd_is_master(mtd))
527 return -EINVAL;
528
529 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
538 return mtd_add_partition(mtd, p.devname, p.start, p.length);
539
540 case BLKPG_DEL_PARTITION:
541
542 if (p.pno < 0)
543 return -EINVAL;
544
545 return mtd_del_partition(mtd, p.pno);
546
547 default:
548 return -EINVAL;
549 }
550}
551#endif
552
553
Arnd Bergmann55929332010-04-27 00:24:05 +0200554static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200556 struct mtd_file_info *mfi = file->private_data;
557 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 void __user *argp = (void __user *)arg;
559 int ret = 0;
560 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200561 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
564
565 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
566 if (cmd & IOC_IN) {
567 if (!access_ok(VERIFY_READ, argp, size))
568 return -EFAULT;
569 }
570 if (cmd & IOC_OUT) {
571 if (!access_ok(VERIFY_WRITE, argp, size))
572 return -EFAULT;
573 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 switch (cmd) {
576 case MEMGETREGIONCOUNT:
577 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
578 return -EFAULT;
579 break;
580
581 case MEMGETREGIONINFO:
582 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700583 uint32_t ur_idx;
584 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700585 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Zev Weissb67c5f82008-09-01 05:02:12 -0700587 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EFAULT;
589
Dan Carpenter5e59be12010-09-08 21:39:56 +0200590 if (ur_idx >= mtd->numeraseregions)
591 return -EINVAL;
592
Zev Weissb67c5f82008-09-01 05:02:12 -0700593 kr = &(mtd->eraseregions[ur_idx]);
594
595 if (put_user(kr->offset, &(ur->offset))
596 || put_user(kr->erasesize, &(ur->erasesize))
597 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 }
602
603 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200604 info.type = mtd->type;
605 info.flags = mtd->flags;
606 info.size = mtd->size;
607 info.erasesize = mtd->erasesize;
608 info.writesize = mtd->writesize;
609 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200610 /* The below fields are obsolete */
611 info.ecctype = -1;
612 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200613 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EFAULT;
615 break;
616
617 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700618 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 {
620 struct erase_info *erase;
621
Al Viroaeb5d722008-09-02 15:28:45 -0400622 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EPERM;
624
Burman Yan95b93a02006-11-15 21:10:29 +0200625 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!erase)
627 ret = -ENOMEM;
628 else {
629 wait_queue_head_t waitq;
630 DECLARE_WAITQUEUE(wait, current);
631
632 init_waitqueue_head(&waitq);
633
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700634 if (cmd == MEMERASE64) {
635 struct erase_info_user64 einfo64;
636
637 if (copy_from_user(&einfo64, argp,
638 sizeof(struct erase_info_user64))) {
639 kfree(erase);
640 return -EFAULT;
641 }
642 erase->addr = einfo64.start;
643 erase->len = einfo64.length;
644 } else {
645 struct erase_info_user einfo32;
646
647 if (copy_from_user(&einfo32, argp,
648 sizeof(struct erase_info_user))) {
649 kfree(erase);
650 return -EFAULT;
651 }
652 erase->addr = einfo32.start;
653 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 erase->mtd = mtd;
656 erase->callback = mtdchar_erase_callback;
657 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 FIXME: Allow INTERRUPTIBLE. Which means
661 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 If the wq_head is on the stack, and we
664 leave because we got interrupted, then the
665 wq_head is no longer there when the
666 callback routine tries to wake us up.
667 */
668 ret = mtd->erase(mtd, erase);
669 if (!ret) {
670 set_current_state(TASK_UNINTERRUPTIBLE);
671 add_wait_queue(&waitq, &wait);
672 if (erase->state != MTD_ERASE_DONE &&
673 erase->state != MTD_ERASE_FAILED)
674 schedule();
675 remove_wait_queue(&waitq, &wait);
676 set_current_state(TASK_RUNNING);
677
678 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
679 }
680 kfree(erase);
681 }
682 break;
683 }
684
685 case MEMWRITEOOB:
686 {
687 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700688 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000689
Kevin Cernekee97718542009-04-08 22:53:13 -0700690 /* NOTE: writes return length to buf_user->length */
691 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700693 else
694 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
695 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 case MEMREADOOB:
700 {
701 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700702 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Kevin Cernekee97718542009-04-08 22:53:13 -0700704 /* NOTE: writes return length to buf_user->start */
705 if (copy_from_user(&buf, argp, sizeof(buf)))
706 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700708 ret = mtd_do_readoob(mtd, buf.start, buf.length,
709 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711 }
712
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700713 case MEMWRITEOOB64:
714 {
715 struct mtd_oob_buf64 buf;
716 struct mtd_oob_buf64 __user *buf_user = argp;
717
718 if (copy_from_user(&buf, argp, sizeof(buf)))
719 ret = -EFAULT;
720 else
721 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
722 (void __user *)(uintptr_t)buf.usr_ptr,
723 &buf_user->length);
724 break;
725 }
726
727 case MEMREADOOB64:
728 {
729 struct mtd_oob_buf64 buf;
730 struct mtd_oob_buf64 __user *buf_user = argp;
731
732 if (copy_from_user(&buf, argp, sizeof(buf)))
733 ret = -EFAULT;
734 else
735 ret = mtd_do_readoob(mtd, buf.start, buf.length,
736 (void __user *)(uintptr_t)buf.usr_ptr,
737 &buf_user->length);
738 break;
739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case MEMLOCK:
742 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700743 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Harvey Harrison175428b2008-07-03 23:40:14 -0700745 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EFAULT;
747
748 if (!mtd->lock)
749 ret = -EOPNOTSUPP;
750 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700751 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 }
754
755 case MEMUNLOCK:
756 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700757 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Harvey Harrison175428b2008-07-03 23:40:14 -0700759 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EFAULT;
761
762 if (!mtd->unlock)
763 ret = -EOPNOTSUPP;
764 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700765 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 }
768
Richard Cochran99384242010-06-14 18:10:33 +0200769 case MEMISLOCKED:
770 {
771 struct erase_info_user einfo;
772
773 if (copy_from_user(&einfo, argp, sizeof(einfo)))
774 return -EFAULT;
775
776 if (!mtd->is_locked)
777 ret = -EOPNOTSUPP;
778 else
779 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
780 break;
781 }
782
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200783 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 case MEMGETOOBSEL:
785 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200786 struct nand_oobinfo oi;
787
788 if (!mtd->ecclayout)
789 return -EOPNOTSUPP;
790 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
791 return -EINVAL;
792
793 oi.useecc = MTD_NANDECC_AUTOPLACE;
794 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
795 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
796 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200797 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200798
799 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EFAULT;
801 break;
802 }
803
804 case MEMGETBADBLOCK:
805 {
806 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (copy_from_user(&offs, argp, sizeof(loff_t)))
809 return -EFAULT;
810 if (!mtd->block_isbad)
811 ret = -EOPNOTSUPP;
812 else
813 return mtd->block_isbad(mtd, offs);
814 break;
815 }
816
817 case MEMSETBADBLOCK:
818 {
819 loff_t offs;
820
821 if (copy_from_user(&offs, argp, sizeof(loff_t)))
822 return -EFAULT;
823 if (!mtd->block_markbad)
824 ret = -EOPNOTSUPP;
825 else
826 return mtd->block_markbad(mtd, offs);
827 break;
828 }
829
David Brownell34a82442008-07-30 12:35:05 -0700830#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000831 case OTPSELECT:
832 {
833 int mode;
834 if (copy_from_user(&mode, argp, sizeof(int)))
835 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200836
837 mfi->mode = MTD_MODE_NORMAL;
838
839 ret = otp_select_filemode(mfi, mode);
840
Nicolas Pitre81dba482005-04-01 16:36:15 +0100841 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000842 break;
843 }
844
845 case OTPGETREGIONCOUNT:
846 case OTPGETREGIONINFO:
847 {
848 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
849 if (!buf)
850 return -ENOMEM;
851 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200852 switch (mfi->mode) {
853 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000854 if (mtd->get_fact_prot_info)
855 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
856 break;
857 case MTD_MODE_OTP_USER:
858 if (mtd->get_user_prot_info)
859 ret = mtd->get_user_prot_info(mtd, buf, 4096);
860 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200861 default:
862 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000863 }
864 if (ret >= 0) {
865 if (cmd == OTPGETREGIONCOUNT) {
866 int nbr = ret / sizeof(struct otp_info);
867 ret = copy_to_user(argp, &nbr, sizeof(int));
868 } else
869 ret = copy_to_user(argp, buf, ret);
870 if (ret)
871 ret = -EFAULT;
872 }
873 kfree(buf);
874 break;
875 }
876
877 case OTPLOCK:
878 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700879 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000880
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200881 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000882 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700883 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000884 return -EFAULT;
885 if (!mtd->lock_user_prot_reg)
886 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700887 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000888 break;
889 }
890#endif
891
Brian Norriscc26c3c2010-08-24 18:12:00 -0700892 /* This ioctl is being deprecated - it truncates the ecc layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200893 case ECCGETLAYOUT:
894 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700895 struct nand_ecclayout_user *usrlay;
896
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200897 if (!mtd->ecclayout)
898 return -EOPNOTSUPP;
899
Brian Norriscc26c3c2010-08-24 18:12:00 -0700900 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
901 if (!usrlay)
902 return -ENOMEM;
903
904 shrink_ecclayout(mtd->ecclayout, usrlay);
905
906 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
907 ret = -EFAULT;
908 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200909 break;
910 }
911
912 case ECCGETSTATS:
913 {
914 if (copy_to_user(argp, &mtd->ecc_stats,
915 sizeof(struct mtd_ecc_stats)))
916 return -EFAULT;
917 break;
918 }
919
920 case MTDFILEMODE:
921 {
922 mfi->mode = 0;
923
924 switch(arg) {
925 case MTD_MODE_OTP_FACTORY:
926 case MTD_MODE_OTP_USER:
927 ret = otp_select_filemode(mfi, arg);
928 break;
929
930 case MTD_MODE_RAW:
931 if (!mtd->read_oob || !mtd->write_oob)
932 return -EOPNOTSUPP;
933 mfi->mode = arg;
934
935 case MTD_MODE_NORMAL:
936 break;
937 default:
938 ret = -EINVAL;
939 }
940 file->f_pos = 0;
941 break;
942 }
943
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300944#ifdef CONFIG_MTD_PARTITIONS
945 case BLKPG:
946 {
947 ret = mtd_blkpg_ioctl(mtd,
948 (struct blkpg_ioctl_arg __user *)arg);
949 break;
950 }
951
952 case BLKRRPART:
953 {
954 /* No reread partition feature. Just return ok */
955 ret = 0;
956 break;
957 }
958#endif
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 default:
961 ret = -ENOTTY;
962 }
963
964 return ret;
965} /* memory_ioctl */
966
Arnd Bergmann55929332010-04-27 00:24:05 +0200967static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
968{
969 int ret;
970
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200971 mutex_lock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200972 ret = mtd_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200973 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200974
975 return ret;
976}
977
Kevin Cernekee97718542009-04-08 22:53:13 -0700978#ifdef CONFIG_COMPAT
979
980struct mtd_oob_buf32 {
981 u_int32_t start;
982 u_int32_t length;
983 compat_caddr_t ptr; /* unsigned char* */
984};
985
986#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
987#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
988
989static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
990 unsigned long arg)
991{
992 struct mtd_file_info *mfi = file->private_data;
993 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100994 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700995 int ret = 0;
996
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200997 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -0700998
999 switch (cmd) {
1000 case MEMWRITEOOB32:
1001 {
1002 struct mtd_oob_buf32 buf;
1003 struct mtd_oob_buf32 __user *buf_user = argp;
1004
1005 if (copy_from_user(&buf, argp, sizeof(buf)))
1006 ret = -EFAULT;
1007 else
1008 ret = mtd_do_writeoob(file, mtd, buf.start,
1009 buf.length, compat_ptr(buf.ptr),
1010 &buf_user->length);
1011 break;
1012 }
1013
1014 case MEMREADOOB32:
1015 {
1016 struct mtd_oob_buf32 buf;
1017 struct mtd_oob_buf32 __user *buf_user = argp;
1018
1019 /* NOTE: writes return length to buf->start */
1020 if (copy_from_user(&buf, argp, sizeof(buf)))
1021 ret = -EFAULT;
1022 else
1023 ret = mtd_do_readoob(mtd, buf.start,
1024 buf.length, compat_ptr(buf.ptr),
1025 &buf_user->start);
1026 break;
1027 }
1028 default:
Arnd Bergmann55929332010-04-27 00:24:05 +02001029 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001030 }
1031
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001032 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001033
1034 return ret;
1035}
1036
1037#endif /* CONFIG_COMPAT */
1038
David Howells402d3262009-02-12 10:40:00 +00001039/*
1040 * try to determine where a shared mapping can be made
1041 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1042 * mappings)
1043 */
1044#ifndef CONFIG_MMU
1045static unsigned long mtd_get_unmapped_area(struct file *file,
1046 unsigned long addr,
1047 unsigned long len,
1048 unsigned long pgoff,
1049 unsigned long flags)
1050{
1051 struct mtd_file_info *mfi = file->private_data;
1052 struct mtd_info *mtd = mfi->mtd;
1053
1054 if (mtd->get_unmapped_area) {
1055 unsigned long offset;
1056
1057 if (addr != 0)
1058 return (unsigned long) -EINVAL;
1059
1060 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1061 return (unsigned long) -EINVAL;
1062
1063 offset = pgoff << PAGE_SHIFT;
1064 if (offset > mtd->size - len)
1065 return (unsigned long) -EINVAL;
1066
1067 return mtd->get_unmapped_area(mtd, len, offset, flags);
1068 }
1069
1070 /* can't map directly */
1071 return (unsigned long) -ENOSYS;
1072}
1073#endif
1074
1075/*
1076 * set up a mapping for shared memory segments
1077 */
1078static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
1079{
1080#ifdef CONFIG_MMU
1081 struct mtd_file_info *mfi = file->private_data;
1082 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001083 struct map_info *map = mtd->priv;
1084 unsigned long start;
1085 unsigned long off;
1086 u32 len;
David Howells402d3262009-02-12 10:40:00 +00001087
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001088 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1089 off = vma->vm_pgoff << PAGE_SHIFT;
1090 start = map->phys;
1091 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1092 start &= PAGE_MASK;
1093 if ((vma->vm_end - vma->vm_start + off) > len)
1094 return -EINVAL;
1095
1096 off += start;
1097 vma->vm_pgoff = off >> PAGE_SHIFT;
1098 vma->vm_flags |= VM_IO | VM_RESERVED;
1099
1100#ifdef pgprot_noncached
1101 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1102 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1103#endif
1104 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1105 vma->vm_end - vma->vm_start,
1106 vma->vm_page_prot))
1107 return -EAGAIN;
1108
David Howells402d3262009-02-12 10:40:00 +00001109 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001110 }
David Howells402d3262009-02-12 10:40:00 +00001111 return -ENOSYS;
1112#else
1113 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1114#endif
1115}
1116
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001117static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .owner = THIS_MODULE,
1119 .llseek = mtd_lseek,
1120 .read = mtd_read,
1121 .write = mtd_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001122 .unlocked_ioctl = mtd_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001123#ifdef CONFIG_COMPAT
1124 .compat_ioctl = mtd_compat_ioctl,
1125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 .open = mtd_open,
1127 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +00001128 .mmap = mtd_mmap,
1129#ifndef CONFIG_MMU
1130 .get_unmapped_area = mtd_get_unmapped_area,
1131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132};
1133
Al Viro51139ad2010-07-25 23:47:46 +04001134static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1135 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001136{
Linus Torvalds79346502010-10-30 08:31:35 -07001137 return mount_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001138}
1139
1140static struct file_system_type mtd_inodefs_type = {
1141 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001142 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001143 .kill_sb = kill_anon_super,
1144};
1145
1146static void mtdchar_notify_add(struct mtd_info *mtd)
1147{
1148}
1149
1150static void mtdchar_notify_remove(struct mtd_info *mtd)
1151{
1152 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1153
1154 if (mtd_ino) {
1155 /* Destroy the inode if it exists */
1156 mtd_ino->i_nlink = 0;
1157 iput(mtd_ino);
1158 }
1159}
1160
1161static struct mtd_notifier mtdchar_notifier = {
1162 .add = mtdchar_notify_add,
1163 .remove = mtdchar_notify_remove,
1164};
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166static int __init init_mtdchar(void)
1167{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001168 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001169
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001170 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001171 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001172 if (ret < 0) {
1173 pr_notice("Can't allocate major number %d for "
1174 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001178 ret = register_filesystem(&mtd_inodefs_type);
1179 if (ret) {
1180 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1181 goto err_unregister_chdev;
1182 }
1183
1184 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1185 if (IS_ERR(mtd_inode_mnt)) {
1186 ret = PTR_ERR(mtd_inode_mnt);
1187 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1188 goto err_unregister_filesystem;
1189 }
1190 register_mtd_user(&mtdchar_notifier);
1191
1192 return ret;
1193
1194err_unregister_filesystem:
1195 unregister_filesystem(&mtd_inodefs_type);
1196err_unregister_chdev:
1197 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static void __exit cleanup_mtdchar(void)
1202{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001203 unregister_mtd_user(&mtdchar_notifier);
Nick Pigginb3e19d92011-01-07 17:50:11 +11001204 mntput_long(mtd_inode_mnt);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001205 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001206 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209module_init(init_mtdchar);
1210module_exit(cleanup_mtdchar);
1211
David Brownell1f24b5a2009-03-26 00:42:41 -07001212MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214MODULE_LICENSE("GPL");
1215MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1216MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001217MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);