blob: 5895de7018d4facfeca14c5f07220cfe525e20ac [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>
Jonathan Corbet60712392008-05-15 10:10:37 -060029#include <linux/smp_lock.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>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mtd/mtd.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020035#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Thomas Gleixner15fdc522005-11-07 00:14:42 +010037#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010038
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030039#define MTD_INODE_FS_MAGIC 0x11307854
40static struct vfsmount *mtd_inode_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Nicolas Pitre045e9a52005-02-08 19:12:53 +000042/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020043 * Data structure to hold the pointer to the mtd device as well
44 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000045 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020046struct mtd_file_info {
47 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030048 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020049 enum mtd_file_modes mode;
50};
Nicolas Pitre31f42332005-02-08 17:45:55 +000051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
53{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020054 struct mtd_file_info *mfi = file->private_data;
55 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040058 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040060 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010061 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040063 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010064 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
66 default:
67 return -EINVAL;
68 }
69
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020070 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010071 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Todd Poynor8b491d72005-08-04 02:05:51 +010073 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76
77
78static int mtd_open(struct inode *inode, struct file *file)
79{
80 int minor = iminor(inode);
81 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060082 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020084 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030085 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040090 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return -EACCES;
92
Jonathan Corbet60712392008-05-15 10:10:37 -060093 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000095
Jonathan Corbet60712392008-05-15 10:10:37 -060096 if (IS_ERR(mtd)) {
97 ret = PTR_ERR(mtd);
98 goto out;
99 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000100
David Howells402d3262009-02-12 10:40:00 +0000101 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600103 ret = -ENODEV;
104 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300107 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
108 if (!mtd_ino) {
109 put_mtd_device(mtd);
110 ret = -ENOMEM;
111 goto out;
112 }
113 if (mtd_ino->i_state & I_NEW) {
114 mtd_ino->i_private = mtd;
115 mtd_ino->i_mode = S_IFCHR;
116 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
117 unlock_new_inode(mtd_ino);
118 }
119 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400122 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300123 iput(mtd_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600125 ret = -EACCES;
126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000128
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200129 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
130 if (!mfi) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300131 iput(mtd_ino);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200132 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600133 ret = -ENOMEM;
134 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200135 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300136 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200137 mfi->mtd = mtd;
138 file->private_data = mfi;
139
Jonathan Corbet60712392008-05-15 10:10:37 -0600140out:
141 unlock_kernel();
142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143} /* mtd_open */
144
145/*====================================================================*/
146
147static int mtd_close(struct inode *inode, struct file *file)
148{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200149 struct mtd_file_info *mfi = file->private_data;
150 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
153
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200154 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400155 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000157
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300158 iput(mfi->ino);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200161 file->private_data = NULL;
162 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return 0;
165} /* mtd_close */
166
167/* FIXME: This _really_ needs to die. In 2.5, we should lock the
168 userspace buffer down and use it directly with readv/writev.
169*/
170#define MAX_KMALLOC_SIZE 0x20000
171
172static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
173{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200174 struct mtd_file_info *mfi = file->private_data;
175 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 size_t retlen=0;
177 size_t total_retlen=0;
178 int ret=0;
179 int len;
180 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
183
184 if (*ppos + count > mtd->size)
185 count = mtd->size - *ppos;
186
187 if (!count)
188 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
191 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100192
193 if (count > MAX_KMALLOC_SIZE)
194 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
195 else
196 kbuf=kmalloc(count, GFP_KERNEL);
197
198 if (!kbuf)
199 return -ENOMEM;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100202
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000203 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 len = MAX_KMALLOC_SIZE;
205 else
206 len = count;
207
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200208 switch (mfi->mode) {
209 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000210 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
211 break;
212 case MTD_MODE_OTP_USER:
213 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
214 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200215 case MTD_MODE_RAW:
216 {
217 struct mtd_oob_ops ops;
218
219 ops.mode = MTD_OOB_RAW;
220 ops.datbuf = kbuf;
221 ops.oobbuf = NULL;
222 ops.len = len;
223
224 ret = mtd->read_oob(mtd, *ppos, &ops);
225 retlen = ops.retlen;
226 break;
227 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000228 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200229 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Nand returns -EBADMSG on ecc errors, but it returns
232 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000233 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200234 * For kernel internal usage it also might return -EUCLEAN
235 * to signal the caller that a bitflip has occured and has
236 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * Userspace software which accesses NAND this way
238 * must be aware of the fact that it deals with NAND
239 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200240 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *ppos += retlen;
242 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200243 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -EFAULT;
245 }
246 else
247 total_retlen += retlen;
248
249 count -= retlen;
250 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000251 if (retlen == 0)
252 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 else {
255 kfree(kbuf);
256 return ret;
257 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Thago Galesib802c072006-04-17 17:38:15 +0100261 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return total_retlen;
263} /* mtd_read */
264
265static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
266{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200267 struct mtd_file_info *mfi = file->private_data;
268 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 char *kbuf;
270 size_t retlen;
271 size_t total_retlen=0;
272 int ret=0;
273 int len;
274
275 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (*ppos == mtd->size)
278 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (*ppos + count > mtd->size)
281 count = mtd->size - *ppos;
282
283 if (!count)
284 return 0;
285
Thago Galesib802c072006-04-17 17:38:15 +0100286 if (count > MAX_KMALLOC_SIZE)
287 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
288 else
289 kbuf=kmalloc(count, GFP_KERNEL);
290
291 if (!kbuf)
292 return -ENOMEM;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100295
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000296 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 len = MAX_KMALLOC_SIZE;
298 else
299 len = count;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (copy_from_user(kbuf, buf, len)) {
302 kfree(kbuf);
303 return -EFAULT;
304 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000305
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200306 switch (mfi->mode) {
307 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000308 ret = -EROFS;
309 break;
310 case MTD_MODE_OTP_USER:
311 if (!mtd->write_user_prot_reg) {
312 ret = -EOPNOTSUPP;
313 break;
314 }
315 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
316 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200317
318 case MTD_MODE_RAW:
319 {
320 struct mtd_oob_ops ops;
321
322 ops.mode = MTD_OOB_RAW;
323 ops.datbuf = kbuf;
324 ops.oobbuf = NULL;
325 ops.len = len;
326
327 ret = mtd->write_oob(mtd, *ppos, &ops);
328 retlen = ops.retlen;
329 break;
330 }
331
Nicolas Pitre31f42332005-02-08 17:45:55 +0000332 default:
333 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!ret) {
336 *ppos += retlen;
337 total_retlen += retlen;
338 count -= retlen;
339 buf += retlen;
340 }
341 else {
342 kfree(kbuf);
343 return ret;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Thago Galesib802c072006-04-17 17:38:15 +0100347 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return total_retlen;
349} /* mtd_write */
350
351/*======================================================================
352
353 IOCTL calls for getting device parameters.
354
355======================================================================*/
356static void mtdchar_erase_callback (struct erase_info *instr)
357{
358 wake_up((wait_queue_head_t *)instr->priv);
359}
360
David Brownell34a82442008-07-30 12:35:05 -0700361#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200362static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
363{
364 struct mtd_info *mtd = mfi->mtd;
365 int ret = 0;
366
367 switch (mode) {
368 case MTD_OTP_FACTORY:
369 if (!mtd->read_fact_prot_reg)
370 ret = -EOPNOTSUPP;
371 else
372 mfi->mode = MTD_MODE_OTP_FACTORY;
373 break;
374 case MTD_OTP_USER:
375 if (!mtd->read_fact_prot_reg)
376 ret = -EOPNOTSUPP;
377 else
378 mfi->mode = MTD_MODE_OTP_USER;
379 break;
380 default:
381 ret = -EINVAL;
382 case MTD_OTP_OFF:
383 break;
384 }
385 return ret;
386}
387#else
388# define otp_select_filemode(f,m) -EOPNOTSUPP
389#endif
390
Kevin Cernekee97718542009-04-08 22:53:13 -0700391static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
392 uint64_t start, uint32_t length, void __user *ptr,
393 uint32_t __user *retp)
394{
395 struct mtd_oob_ops ops;
396 uint32_t retlen;
397 int ret = 0;
398
399 if (!(file->f_mode & FMODE_WRITE))
400 return -EPERM;
401
402 if (length > 4096)
403 return -EINVAL;
404
405 if (!mtd->write_oob)
406 ret = -EOPNOTSUPP;
407 else
Roel Kluin00404762010-01-29 10:35:04 +0100408 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700409
410 if (ret)
411 return ret;
412
413 ops.ooblen = length;
414 ops.ooboffs = start & (mtd->oobsize - 1);
415 ops.datbuf = NULL;
416 ops.mode = MTD_OOB_PLACE;
417
418 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
419 return -EINVAL;
420
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200421 ops.oobbuf = memdup_user(ptr, length);
422 if (IS_ERR(ops.oobbuf))
423 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700424
425 start &= ~((uint64_t)mtd->oobsize - 1);
426 ret = mtd->write_oob(mtd, start, &ops);
427
428 if (ops.oobretlen > 0xFFFFFFFFU)
429 ret = -EOVERFLOW;
430 retlen = ops.oobretlen;
431 if (copy_to_user(retp, &retlen, sizeof(length)))
432 ret = -EFAULT;
433
434 kfree(ops.oobbuf);
435 return ret;
436}
437
438static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
439 uint32_t length, void __user *ptr, uint32_t __user *retp)
440{
441 struct mtd_oob_ops ops;
442 int ret = 0;
443
444 if (length > 4096)
445 return -EINVAL;
446
447 if (!mtd->read_oob)
448 ret = -EOPNOTSUPP;
449 else
450 ret = access_ok(VERIFY_WRITE, ptr,
451 length) ? 0 : -EFAULT;
452 if (ret)
453 return ret;
454
455 ops.ooblen = length;
456 ops.ooboffs = start & (mtd->oobsize - 1);
457 ops.datbuf = NULL;
458 ops.mode = MTD_OOB_PLACE;
459
460 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
461 return -EINVAL;
462
463 ops.oobbuf = kmalloc(length, GFP_KERNEL);
464 if (!ops.oobbuf)
465 return -ENOMEM;
466
467 start &= ~((uint64_t)mtd->oobsize - 1);
468 ret = mtd->read_oob(mtd, start, &ops);
469
470 if (put_user(ops.oobretlen, retp))
471 ret = -EFAULT;
472 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
473 ops.oobretlen))
474 ret = -EFAULT;
475
476 kfree(ops.oobbuf);
477 return ret;
478}
479
Brian Norriscc26c3c2010-08-24 18:12:00 -0700480/*
481 * Copies (and truncates, if necessary) data from the larger struct,
482 * nand_ecclayout, to the smaller, deprecated layout struct,
483 * nand_ecclayout_user. This is necessary only to suppport the deprecated
484 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
485 * nand_ecclayout flexibly (i.e. the struct may change size in new
486 * releases without requiring major rewrites).
487 */
488static int shrink_ecclayout(const struct nand_ecclayout *from,
489 struct nand_ecclayout_user *to)
490{
491 int i;
492
493 if (!from || !to)
494 return -EINVAL;
495
496 memset(to, 0, sizeof(*to));
497
Brian Norris0ceacf32010-09-19 23:57:12 -0700498 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700499 for (i = 0; i < to->eccbytes; i++)
500 to->eccpos[i] = from->eccpos[i];
501
502 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
503 if (from->oobfree[i].length == 0 &&
504 from->oobfree[i].offset == 0)
505 break;
506 to->oobavail += from->oobfree[i].length;
507 to->oobfree[i] = from->oobfree[i];
508 }
509
510 return 0;
511}
512
Arnd Bergmann55929332010-04-27 00:24:05 +0200513static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200515 struct mtd_file_info *mfi = file->private_data;
516 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 void __user *argp = (void __user *)arg;
518 int ret = 0;
519 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200520 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
523
524 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
525 if (cmd & IOC_IN) {
526 if (!access_ok(VERIFY_READ, argp, size))
527 return -EFAULT;
528 }
529 if (cmd & IOC_OUT) {
530 if (!access_ok(VERIFY_WRITE, argp, size))
531 return -EFAULT;
532 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 switch (cmd) {
535 case MEMGETREGIONCOUNT:
536 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
537 return -EFAULT;
538 break;
539
540 case MEMGETREGIONINFO:
541 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700542 uint32_t ur_idx;
543 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700544 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Zev Weissb67c5f82008-09-01 05:02:12 -0700546 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -EFAULT;
548
Dan Carpenter5e59be12010-09-08 21:39:56 +0200549 if (ur_idx >= mtd->numeraseregions)
550 return -EINVAL;
551
Zev Weissb67c5f82008-09-01 05:02:12 -0700552 kr = &(mtd->eraseregions[ur_idx]);
553
554 if (put_user(kr->offset, &(ur->offset))
555 || put_user(kr->erasesize, &(ur->erasesize))
556 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 }
561
562 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200563 info.type = mtd->type;
564 info.flags = mtd->flags;
565 info.size = mtd->size;
566 info.erasesize = mtd->erasesize;
567 info.writesize = mtd->writesize;
568 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200569 /* The below fields are obsolete */
570 info.ecctype = -1;
571 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200572 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EFAULT;
574 break;
575
576 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700577 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 {
579 struct erase_info *erase;
580
Al Viroaeb5d722008-09-02 15:28:45 -0400581 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -EPERM;
583
Burman Yan95b93a02006-11-15 21:10:29 +0200584 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (!erase)
586 ret = -ENOMEM;
587 else {
588 wait_queue_head_t waitq;
589 DECLARE_WAITQUEUE(wait, current);
590
591 init_waitqueue_head(&waitq);
592
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700593 if (cmd == MEMERASE64) {
594 struct erase_info_user64 einfo64;
595
596 if (copy_from_user(&einfo64, argp,
597 sizeof(struct erase_info_user64))) {
598 kfree(erase);
599 return -EFAULT;
600 }
601 erase->addr = einfo64.start;
602 erase->len = einfo64.length;
603 } else {
604 struct erase_info_user einfo32;
605
606 if (copy_from_user(&einfo32, argp,
607 sizeof(struct erase_info_user))) {
608 kfree(erase);
609 return -EFAULT;
610 }
611 erase->addr = einfo32.start;
612 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 erase->mtd = mtd;
615 erase->callback = mtdchar_erase_callback;
616 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 FIXME: Allow INTERRUPTIBLE. Which means
620 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 If the wq_head is on the stack, and we
623 leave because we got interrupted, then the
624 wq_head is no longer there when the
625 callback routine tries to wake us up.
626 */
627 ret = mtd->erase(mtd, erase);
628 if (!ret) {
629 set_current_state(TASK_UNINTERRUPTIBLE);
630 add_wait_queue(&waitq, &wait);
631 if (erase->state != MTD_ERASE_DONE &&
632 erase->state != MTD_ERASE_FAILED)
633 schedule();
634 remove_wait_queue(&waitq, &wait);
635 set_current_state(TASK_RUNNING);
636
637 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
638 }
639 kfree(erase);
640 }
641 break;
642 }
643
644 case MEMWRITEOOB:
645 {
646 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700647 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000648
Kevin Cernekee97718542009-04-08 22:53:13 -0700649 /* NOTE: writes return length to buf_user->length */
650 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700652 else
653 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
654 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658 case MEMREADOOB:
659 {
660 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700661 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Kevin Cernekee97718542009-04-08 22:53:13 -0700663 /* NOTE: writes return length to buf_user->start */
664 if (copy_from_user(&buf, argp, sizeof(buf)))
665 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700667 ret = mtd_do_readoob(mtd, buf.start, buf.length,
668 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
670 }
671
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700672 case MEMWRITEOOB64:
673 {
674 struct mtd_oob_buf64 buf;
675 struct mtd_oob_buf64 __user *buf_user = argp;
676
677 if (copy_from_user(&buf, argp, sizeof(buf)))
678 ret = -EFAULT;
679 else
680 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
681 (void __user *)(uintptr_t)buf.usr_ptr,
682 &buf_user->length);
683 break;
684 }
685
686 case MEMREADOOB64:
687 {
688 struct mtd_oob_buf64 buf;
689 struct mtd_oob_buf64 __user *buf_user = argp;
690
691 if (copy_from_user(&buf, argp, sizeof(buf)))
692 ret = -EFAULT;
693 else
694 ret = mtd_do_readoob(mtd, buf.start, buf.length,
695 (void __user *)(uintptr_t)buf.usr_ptr,
696 &buf_user->length);
697 break;
698 }
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 case MEMLOCK:
701 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700702 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Harvey Harrison175428b2008-07-03 23:40:14 -0700704 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EFAULT;
706
707 if (!mtd->lock)
708 ret = -EOPNOTSUPP;
709 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700710 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 }
713
714 case MEMUNLOCK:
715 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700716 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Harvey Harrison175428b2008-07-03 23:40:14 -0700718 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -EFAULT;
720
721 if (!mtd->unlock)
722 ret = -EOPNOTSUPP;
723 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700724 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
726 }
727
Richard Cochran99384242010-06-14 18:10:33 +0200728 case MEMISLOCKED:
729 {
730 struct erase_info_user einfo;
731
732 if (copy_from_user(&einfo, argp, sizeof(einfo)))
733 return -EFAULT;
734
735 if (!mtd->is_locked)
736 ret = -EOPNOTSUPP;
737 else
738 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
739 break;
740 }
741
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200742 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case MEMGETOOBSEL:
744 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200745 struct nand_oobinfo oi;
746
747 if (!mtd->ecclayout)
748 return -EOPNOTSUPP;
749 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
750 return -EINVAL;
751
752 oi.useecc = MTD_NANDECC_AUTOPLACE;
753 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
754 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
755 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200756 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200757
758 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EFAULT;
760 break;
761 }
762
763 case MEMGETBADBLOCK:
764 {
765 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (copy_from_user(&offs, argp, sizeof(loff_t)))
768 return -EFAULT;
769 if (!mtd->block_isbad)
770 ret = -EOPNOTSUPP;
771 else
772 return mtd->block_isbad(mtd, offs);
773 break;
774 }
775
776 case MEMSETBADBLOCK:
777 {
778 loff_t offs;
779
780 if (copy_from_user(&offs, argp, sizeof(loff_t)))
781 return -EFAULT;
782 if (!mtd->block_markbad)
783 ret = -EOPNOTSUPP;
784 else
785 return mtd->block_markbad(mtd, offs);
786 break;
787 }
788
David Brownell34a82442008-07-30 12:35:05 -0700789#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000790 case OTPSELECT:
791 {
792 int mode;
793 if (copy_from_user(&mode, argp, sizeof(int)))
794 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200795
796 mfi->mode = MTD_MODE_NORMAL;
797
798 ret = otp_select_filemode(mfi, mode);
799
Nicolas Pitre81dba482005-04-01 16:36:15 +0100800 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000801 break;
802 }
803
804 case OTPGETREGIONCOUNT:
805 case OTPGETREGIONINFO:
806 {
807 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
808 if (!buf)
809 return -ENOMEM;
810 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200811 switch (mfi->mode) {
812 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000813 if (mtd->get_fact_prot_info)
814 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
815 break;
816 case MTD_MODE_OTP_USER:
817 if (mtd->get_user_prot_info)
818 ret = mtd->get_user_prot_info(mtd, buf, 4096);
819 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200820 default:
821 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000822 }
823 if (ret >= 0) {
824 if (cmd == OTPGETREGIONCOUNT) {
825 int nbr = ret / sizeof(struct otp_info);
826 ret = copy_to_user(argp, &nbr, sizeof(int));
827 } else
828 ret = copy_to_user(argp, buf, ret);
829 if (ret)
830 ret = -EFAULT;
831 }
832 kfree(buf);
833 break;
834 }
835
836 case OTPLOCK:
837 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700838 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000839
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200840 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000841 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700842 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000843 return -EFAULT;
844 if (!mtd->lock_user_prot_reg)
845 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700846 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000847 break;
848 }
849#endif
850
Brian Norriscc26c3c2010-08-24 18:12:00 -0700851 /* This ioctl is being deprecated - it truncates the ecc layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200852 case ECCGETLAYOUT:
853 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700854 struct nand_ecclayout_user *usrlay;
855
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200856 if (!mtd->ecclayout)
857 return -EOPNOTSUPP;
858
Brian Norriscc26c3c2010-08-24 18:12:00 -0700859 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
860 if (!usrlay)
861 return -ENOMEM;
862
863 shrink_ecclayout(mtd->ecclayout, usrlay);
864
865 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
866 ret = -EFAULT;
867 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200868 break;
869 }
870
871 case ECCGETSTATS:
872 {
873 if (copy_to_user(argp, &mtd->ecc_stats,
874 sizeof(struct mtd_ecc_stats)))
875 return -EFAULT;
876 break;
877 }
878
879 case MTDFILEMODE:
880 {
881 mfi->mode = 0;
882
883 switch(arg) {
884 case MTD_MODE_OTP_FACTORY:
885 case MTD_MODE_OTP_USER:
886 ret = otp_select_filemode(mfi, arg);
887 break;
888
889 case MTD_MODE_RAW:
890 if (!mtd->read_oob || !mtd->write_oob)
891 return -EOPNOTSUPP;
892 mfi->mode = arg;
893
894 case MTD_MODE_NORMAL:
895 break;
896 default:
897 ret = -EINVAL;
898 }
899 file->f_pos = 0;
900 break;
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 default:
904 ret = -ENOTTY;
905 }
906
907 return ret;
908} /* memory_ioctl */
909
Arnd Bergmann55929332010-04-27 00:24:05 +0200910static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
911{
912 int ret;
913
914 lock_kernel();
915 ret = mtd_ioctl(file, cmd, arg);
916 unlock_kernel();
917
918 return ret;
919}
920
Kevin Cernekee97718542009-04-08 22:53:13 -0700921#ifdef CONFIG_COMPAT
922
923struct mtd_oob_buf32 {
924 u_int32_t start;
925 u_int32_t length;
926 compat_caddr_t ptr; /* unsigned char* */
927};
928
929#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
930#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
931
932static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
933 unsigned long arg)
934{
935 struct mtd_file_info *mfi = file->private_data;
936 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100937 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700938 int ret = 0;
939
940 lock_kernel();
941
942 switch (cmd) {
943 case MEMWRITEOOB32:
944 {
945 struct mtd_oob_buf32 buf;
946 struct mtd_oob_buf32 __user *buf_user = argp;
947
948 if (copy_from_user(&buf, argp, sizeof(buf)))
949 ret = -EFAULT;
950 else
951 ret = mtd_do_writeoob(file, mtd, buf.start,
952 buf.length, compat_ptr(buf.ptr),
953 &buf_user->length);
954 break;
955 }
956
957 case MEMREADOOB32:
958 {
959 struct mtd_oob_buf32 buf;
960 struct mtd_oob_buf32 __user *buf_user = argp;
961
962 /* NOTE: writes return length to buf->start */
963 if (copy_from_user(&buf, argp, sizeof(buf)))
964 ret = -EFAULT;
965 else
966 ret = mtd_do_readoob(mtd, buf.start,
967 buf.length, compat_ptr(buf.ptr),
968 &buf_user->start);
969 break;
970 }
971 default:
Arnd Bergmann55929332010-04-27 00:24:05 +0200972 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -0700973 }
974
975 unlock_kernel();
976
977 return ret;
978}
979
980#endif /* CONFIG_COMPAT */
981
David Howells402d3262009-02-12 10:40:00 +0000982/*
983 * try to determine where a shared mapping can be made
984 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
985 * mappings)
986 */
987#ifndef CONFIG_MMU
988static unsigned long mtd_get_unmapped_area(struct file *file,
989 unsigned long addr,
990 unsigned long len,
991 unsigned long pgoff,
992 unsigned long flags)
993{
994 struct mtd_file_info *mfi = file->private_data;
995 struct mtd_info *mtd = mfi->mtd;
996
997 if (mtd->get_unmapped_area) {
998 unsigned long offset;
999
1000 if (addr != 0)
1001 return (unsigned long) -EINVAL;
1002
1003 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1004 return (unsigned long) -EINVAL;
1005
1006 offset = pgoff << PAGE_SHIFT;
1007 if (offset > mtd->size - len)
1008 return (unsigned long) -EINVAL;
1009
1010 return mtd->get_unmapped_area(mtd, len, offset, flags);
1011 }
1012
1013 /* can't map directly */
1014 return (unsigned long) -ENOSYS;
1015}
1016#endif
1017
1018/*
1019 * set up a mapping for shared memory segments
1020 */
1021static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
1022{
1023#ifdef CONFIG_MMU
1024 struct mtd_file_info *mfi = file->private_data;
1025 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001026 struct map_info *map = mtd->priv;
1027 unsigned long start;
1028 unsigned long off;
1029 u32 len;
David Howells402d3262009-02-12 10:40:00 +00001030
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001031 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1032 off = vma->vm_pgoff << PAGE_SHIFT;
1033 start = map->phys;
1034 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1035 start &= PAGE_MASK;
1036 if ((vma->vm_end - vma->vm_start + off) > len)
1037 return -EINVAL;
1038
1039 off += start;
1040 vma->vm_pgoff = off >> PAGE_SHIFT;
1041 vma->vm_flags |= VM_IO | VM_RESERVED;
1042
1043#ifdef pgprot_noncached
1044 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1045 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1046#endif
1047 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1048 vma->vm_end - vma->vm_start,
1049 vma->vm_page_prot))
1050 return -EAGAIN;
1051
David Howells402d3262009-02-12 10:40:00 +00001052 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001053 }
David Howells402d3262009-02-12 10:40:00 +00001054 return -ENOSYS;
1055#else
1056 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1057#endif
1058}
1059
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001060static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 .owner = THIS_MODULE,
1062 .llseek = mtd_lseek,
1063 .read = mtd_read,
1064 .write = mtd_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001065 .unlocked_ioctl = mtd_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001066#ifdef CONFIG_COMPAT
1067 .compat_ioctl = mtd_compat_ioctl,
1068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .open = mtd_open,
1070 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +00001071 .mmap = mtd_mmap,
1072#ifndef CONFIG_MMU
1073 .get_unmapped_area = mtd_get_unmapped_area,
1074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075};
1076
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001077static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
1078 const char *dev_name, void *data,
1079 struct vfsmount *mnt)
1080{
1081 return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
1082 mnt);
1083}
1084
1085static struct file_system_type mtd_inodefs_type = {
1086 .name = "mtd_inodefs",
1087 .get_sb = mtd_inodefs_get_sb,
1088 .kill_sb = kill_anon_super,
1089};
1090
1091static void mtdchar_notify_add(struct mtd_info *mtd)
1092{
1093}
1094
1095static void mtdchar_notify_remove(struct mtd_info *mtd)
1096{
1097 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1098
1099 if (mtd_ino) {
1100 /* Destroy the inode if it exists */
1101 mtd_ino->i_nlink = 0;
1102 iput(mtd_ino);
1103 }
1104}
1105
1106static struct mtd_notifier mtdchar_notifier = {
1107 .add = mtdchar_notify_add,
1108 .remove = mtdchar_notify_remove,
1109};
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111static int __init init_mtdchar(void)
1112{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001113 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001114
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001115 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001116 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001117 if (ret < 0) {
1118 pr_notice("Can't allocate major number %d for "
1119 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001123 ret = register_filesystem(&mtd_inodefs_type);
1124 if (ret) {
1125 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1126 goto err_unregister_chdev;
1127 }
1128
1129 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1130 if (IS_ERR(mtd_inode_mnt)) {
1131 ret = PTR_ERR(mtd_inode_mnt);
1132 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1133 goto err_unregister_filesystem;
1134 }
1135 register_mtd_user(&mtdchar_notifier);
1136
1137 return ret;
1138
1139err_unregister_filesystem:
1140 unregister_filesystem(&mtd_inodefs_type);
1141err_unregister_chdev:
1142 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void __exit cleanup_mtdchar(void)
1147{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001148 unregister_mtd_user(&mtdchar_notifier);
1149 mntput(mtd_inode_mnt);
1150 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001151 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154module_init(init_mtdchar);
1155module_exit(cleanup_mtdchar);
1156
David Brownell1f24b5a2009-03-26 00:42:41 -07001157MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159MODULE_LICENSE("GPL");
1160MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1161MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001162MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);