blob: ed9468c3a1cc507b9f239ea57a97d206dc204d5d [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>
Murali Palnati3e8c3bf2012-02-15 16:51:17 +053037#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Thomas Gleixner15fdc522005-11-07 00:14:42 +010039#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010040
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030041#define MTD_INODE_FS_MAGIC 0x11307854
Arnd Bergmann5aa82942010-06-02 14:28:52 +020042static DEFINE_MUTEX(mtd_mutex);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030043static struct vfsmount *mtd_inode_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Nicolas Pitre045e9a52005-02-08 19:12:53 +000045/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020046 * Data structure to hold the pointer to the mtd device as well
47 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000048 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020049struct mtd_file_info {
50 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030051 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052 enum mtd_file_modes mode;
53};
Nicolas Pitre31f42332005-02-08 17:45:55 +000054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
56{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020057 struct mtd_file_info *mfi = file->private_data;
58 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040061 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040063 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010064 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040066 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010067 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 default:
70 return -EINVAL;
71 }
72
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020073 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010074 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Todd Poynor8b491d72005-08-04 02:05:51 +010076 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79
80
81static int mtd_open(struct inode *inode, struct file *file)
82{
83 int minor = iminor(inode);
84 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060085 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020087 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030088 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040093 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -EACCES;
95
Arnd Bergmann5aa82942010-06-02 14:28:52 +020096 mutex_lock(&mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000098
Jonathan Corbet60712392008-05-15 10:10:37 -060099 if (IS_ERR(mtd)) {
100 ret = PTR_ERR(mtd);
101 goto out;
102 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000103
David Howells402d3262009-02-12 10:40:00 +0000104 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600106 ret = -ENODEV;
107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300110 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
111 if (!mtd_ino) {
112 put_mtd_device(mtd);
113 ret = -ENOMEM;
114 goto out;
115 }
116 if (mtd_ino->i_state & I_NEW) {
117 mtd_ino->i_private = mtd;
118 mtd_ino->i_mode = S_IFCHR;
119 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
120 unlock_new_inode(mtd_ino);
121 }
122 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400125 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300126 iput(mtd_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600128 ret = -EACCES;
129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000131
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200132 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
133 if (!mfi) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300134 iput(mtd_ino);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200135 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600136 ret = -ENOMEM;
137 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200138 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300139 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200140 mfi->mtd = mtd;
141 file->private_data = mfi;
142
Jonathan Corbet60712392008-05-15 10:10:37 -0600143out:
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200144 mutex_unlock(&mtd_mutex);
Jonathan Corbet60712392008-05-15 10:10:37 -0600145 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146} /* mtd_open */
147
148/*====================================================================*/
149
150static int mtd_close(struct inode *inode, struct file *file)
151{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200152 struct mtd_file_info *mfi = file->private_data;
153 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
156
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200157 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400158 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000160
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300161 iput(mfi->ino);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200164 file->private_data = NULL;
165 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return 0;
168} /* mtd_close */
169
Grant Erickson3e45cf52011-04-08 08:51:33 -0700170/* Back in June 2001, dwmw2 wrote:
171 *
172 * FIXME: This _really_ needs to die. In 2.5, we should lock the
173 * userspace buffer down and use it directly with readv/writev.
174 *
175 * The implementation below, using mtd_kmalloc_up_to, mitigates
176 * allocation failures when the system is under low-memory situations
177 * or if memory is highly fragmented at the cost of reducing the
178 * performance of the requested transfer due to a smaller buffer size.
179 *
180 * A more complex but more memory-efficient implementation based on
181 * get_user_pages and iovecs to cover extents of those pages is a
182 * longer-term goal, as intimated by dwmw2 above. However, for the
183 * write case, this requires yet more complex head and tail transfer
184 * handling when those head and tail offsets and sizes are such that
185 * alignment requirements are not met in the NAND subdriver.
186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
189{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
200
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) {
215 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000216 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
217 break;
218 case MTD_MODE_OTP_USER:
219 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
220 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200221 case MTD_MODE_RAW:
222 {
223 struct mtd_oob_ops ops;
224
225 ops.mode = MTD_OOB_RAW;
226 ops.datbuf = kbuf;
227 ops.oobbuf = NULL;
228 ops.len = len;
229
230 ret = mtd->read_oob(mtd, *ppos, &ops);
231 retlen = ops.retlen;
232 break;
233 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000234 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200235 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* Nand returns -EBADMSG on ecc errors, but it returns
238 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000239 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200240 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300241 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200242 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * Userspace software which accesses NAND this way
244 * must be aware of the fact that it deals with NAND
245 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200246 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *ppos += retlen;
248 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200249 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EFAULT;
251 }
252 else
253 total_retlen += retlen;
254
255 count -= retlen;
256 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000257 if (retlen == 0)
258 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 else {
261 kfree(kbuf);
262 return ret;
263 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Thago Galesib802c072006-04-17 17:38:15 +0100267 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return total_retlen;
269} /* mtd_read */
270
271static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
272{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200273 struct mtd_file_info *mfi = file->private_data;
274 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700275 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 char *kbuf;
277 size_t retlen;
278 size_t total_retlen=0;
279 int ret=0;
280 int len;
281
282 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (*ppos == mtd->size)
285 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (*ppos + count > mtd->size)
288 count = mtd->size - *ppos;
289
290 if (!count)
291 return 0;
292
Grant Erickson3e45cf52011-04-08 08:51:33 -0700293 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100294 if (!kbuf)
295 return -ENOMEM;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700298 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (copy_from_user(kbuf, buf, len)) {
301 kfree(kbuf);
302 return -EFAULT;
303 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000304
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200305 switch (mfi->mode) {
306 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000307 ret = -EROFS;
308 break;
309 case MTD_MODE_OTP_USER:
310 if (!mtd->write_user_prot_reg) {
311 ret = -EOPNOTSUPP;
312 break;
313 }
314 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
315 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200316
317 case MTD_MODE_RAW:
318 {
319 struct mtd_oob_ops ops;
320
321 ops.mode = MTD_OOB_RAW;
322 ops.datbuf = kbuf;
323 ops.oobbuf = NULL;
324 ops.len = len;
325
326 ret = mtd->write_oob(mtd, *ppos, &ops);
327 retlen = ops.retlen;
328 break;
329 }
330
Nicolas Pitre31f42332005-02-08 17:45:55 +0000331 default:
332 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (!ret) {
335 *ppos += retlen;
336 total_retlen += retlen;
337 count -= retlen;
338 buf += retlen;
339 }
340 else {
341 kfree(kbuf);
342 return ret;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Thago Galesib802c072006-04-17 17:38:15 +0100346 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return total_retlen;
348} /* mtd_write */
349
350/*======================================================================
351
352 IOCTL calls for getting device parameters.
353
354======================================================================*/
355static void mtdchar_erase_callback (struct erase_info *instr)
356{
357 wake_up((wait_queue_head_t *)instr->priv);
358}
359
David Brownell34a82442008-07-30 12:35:05 -0700360#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200361static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
362{
363 struct mtd_info *mtd = mfi->mtd;
364 int ret = 0;
365
366 switch (mode) {
367 case MTD_OTP_FACTORY:
368 if (!mtd->read_fact_prot_reg)
369 ret = -EOPNOTSUPP;
370 else
371 mfi->mode = MTD_MODE_OTP_FACTORY;
372 break;
373 case MTD_OTP_USER:
374 if (!mtd->read_fact_prot_reg)
375 ret = -EOPNOTSUPP;
376 else
377 mfi->mode = MTD_MODE_OTP_USER;
378 break;
379 default:
380 ret = -EINVAL;
381 case MTD_OTP_OFF:
382 break;
383 }
384 return ret;
385}
386#else
387# define otp_select_filemode(f,m) -EOPNOTSUPP
388#endif
389
Kevin Cernekee97718542009-04-08 22:53:13 -0700390static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
391 uint64_t start, uint32_t length, void __user *ptr,
392 uint32_t __user *retp)
393{
394 struct mtd_oob_ops ops;
395 uint32_t retlen;
396 int ret = 0;
397
398 if (!(file->f_mode & FMODE_WRITE))
399 return -EPERM;
400
401 if (length > 4096)
402 return -EINVAL;
403
404 if (!mtd->write_oob)
405 ret = -EOPNOTSUPP;
406 else
Roel Kluin00404762010-01-29 10:35:04 +0100407 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700408
409 if (ret)
410 return ret;
411
412 ops.ooblen = length;
413 ops.ooboffs = start & (mtd->oobsize - 1);
414 ops.datbuf = NULL;
415 ops.mode = MTD_OOB_PLACE;
416
417 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
418 return -EINVAL;
419
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200420 ops.oobbuf = memdup_user(ptr, length);
421 if (IS_ERR(ops.oobbuf))
422 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700423
424 start &= ~((uint64_t)mtd->oobsize - 1);
425 ret = mtd->write_oob(mtd, start, &ops);
426
427 if (ops.oobretlen > 0xFFFFFFFFU)
428 ret = -EOVERFLOW;
429 retlen = ops.oobretlen;
430 if (copy_to_user(retp, &retlen, sizeof(length)))
431 ret = -EFAULT;
432
433 kfree(ops.oobbuf);
434 return ret;
435}
436
437static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
438 uint32_t length, void __user *ptr, uint32_t __user *retp)
439{
440 struct mtd_oob_ops ops;
441 int ret = 0;
442
443 if (length > 4096)
444 return -EINVAL;
445
446 if (!mtd->read_oob)
447 ret = -EOPNOTSUPP;
448 else
449 ret = access_ok(VERIFY_WRITE, ptr,
450 length) ? 0 : -EFAULT;
451 if (ret)
452 return ret;
453
454 ops.ooblen = length;
455 ops.ooboffs = start & (mtd->oobsize - 1);
456 ops.datbuf = NULL;
457 ops.mode = MTD_OOB_PLACE;
458
459 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
460 return -EINVAL;
461
462 ops.oobbuf = kmalloc(length, GFP_KERNEL);
463 if (!ops.oobbuf)
464 return -ENOMEM;
465
466 start &= ~((uint64_t)mtd->oobsize - 1);
467 ret = mtd->read_oob(mtd, start, &ops);
468
469 if (put_user(ops.oobretlen, retp))
470 ret = -EFAULT;
471 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
472 ops.oobretlen))
473 ret = -EFAULT;
474
475 kfree(ops.oobbuf);
476 return ret;
477}
478
Brian Norriscc26c3c2010-08-24 18:12:00 -0700479/*
480 * Copies (and truncates, if necessary) data from the larger struct,
481 * nand_ecclayout, to the smaller, deprecated layout struct,
482 * nand_ecclayout_user. This is necessary only to suppport the deprecated
483 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
484 * nand_ecclayout flexibly (i.e. the struct may change size in new
485 * releases without requiring major rewrites).
486 */
487static int shrink_ecclayout(const struct nand_ecclayout *from,
488 struct nand_ecclayout_user *to)
489{
490 int i;
491
492 if (!from || !to)
493 return -EINVAL;
494
495 memset(to, 0, sizeof(*to));
496
Brian Norris0ceacf32010-09-19 23:57:12 -0700497 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700498 for (i = 0; i < to->eccbytes; i++)
499 to->eccpos[i] = from->eccpos[i];
500
501 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
502 if (from->oobfree[i].length == 0 &&
503 from->oobfree[i].offset == 0)
504 break;
505 to->oobavail += from->oobfree[i].length;
506 to->oobfree[i] = from->oobfree[i];
507 }
508
509 return 0;
510}
511
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300512static int mtd_blkpg_ioctl(struct mtd_info *mtd,
513 struct blkpg_ioctl_arg __user *arg)
514{
515 struct blkpg_ioctl_arg a;
516 struct blkpg_partition p;
517
518 if (!capable(CAP_SYS_ADMIN))
519 return -EPERM;
520
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300521 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
522 return -EFAULT;
523
524 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
525 return -EFAULT;
526
527 switch (a.op) {
528 case BLKPG_ADD_PARTITION:
529
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200530 /* Only master mtd device must be used to add partitions */
531 if (mtd_is_partition(mtd))
532 return -EINVAL;
533
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300534 return mtd_add_partition(mtd, p.devname, p.start, p.length);
535
536 case BLKPG_DEL_PARTITION:
537
538 if (p.pno < 0)
539 return -EINVAL;
540
541 return mtd_del_partition(mtd, p.pno);
542
543 default:
544 return -EINVAL;
545 }
546}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300547
Arnd Bergmann55929332010-04-27 00:24:05 +0200548static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200550 struct mtd_file_info *mfi = file->private_data;
551 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 void __user *argp = (void __user *)arg;
553 int ret = 0;
554 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200555 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
558
559 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
560 if (cmd & IOC_IN) {
561 if (!access_ok(VERIFY_READ, argp, size))
562 return -EFAULT;
563 }
564 if (cmd & IOC_OUT) {
565 if (!access_ok(VERIFY_WRITE, argp, size))
566 return -EFAULT;
567 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 switch (cmd) {
570 case MEMGETREGIONCOUNT:
571 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
572 return -EFAULT;
573 break;
574
575 case MEMGETREGIONINFO:
576 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700577 uint32_t ur_idx;
578 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700579 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Zev Weissb67c5f82008-09-01 05:02:12 -0700581 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return -EFAULT;
583
Dan Carpenter5e59be12010-09-08 21:39:56 +0200584 if (ur_idx >= mtd->numeraseregions)
585 return -EINVAL;
586
Zev Weissb67c5f82008-09-01 05:02:12 -0700587 kr = &(mtd->eraseregions[ur_idx]);
588
589 if (put_user(kr->offset, &(ur->offset))
590 || put_user(kr->erasesize, &(ur->erasesize))
591 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595 }
596
597 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300598 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200599 info.type = mtd->type;
600 info.flags = mtd->flags;
601 info.size = mtd->size;
602 info.erasesize = mtd->erasesize;
603 info.writesize = mtd->writesize;
604 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200605 /* The below fields are obsolete */
606 info.ecctype = -1;
Joern Engel73c619e2006-05-30 14:25:35 +0200607 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EFAULT;
609 break;
610
611 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700612 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 {
614 struct erase_info *erase;
615
Al Viroaeb5d722008-09-02 15:28:45 -0400616 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EPERM;
618
Burman Yan95b93a02006-11-15 21:10:29 +0200619 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!erase)
621 ret = -ENOMEM;
622 else {
623 wait_queue_head_t waitq;
624 DECLARE_WAITQUEUE(wait, current);
625
626 init_waitqueue_head(&waitq);
627
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700628 if (cmd == MEMERASE64) {
629 struct erase_info_user64 einfo64;
630
631 if (copy_from_user(&einfo64, argp,
632 sizeof(struct erase_info_user64))) {
633 kfree(erase);
634 return -EFAULT;
635 }
636 erase->addr = einfo64.start;
637 erase->len = einfo64.length;
638 } else {
639 struct erase_info_user einfo32;
640
641 if (copy_from_user(&einfo32, argp,
642 sizeof(struct erase_info_user))) {
643 kfree(erase);
644 return -EFAULT;
645 }
646 erase->addr = einfo32.start;
647 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 erase->mtd = mtd;
650 erase->callback = mtdchar_erase_callback;
651 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 FIXME: Allow INTERRUPTIBLE. Which means
655 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 If the wq_head is on the stack, and we
658 leave because we got interrupted, then the
659 wq_head is no longer there when the
660 callback routine tries to wake us up.
661 */
662 ret = mtd->erase(mtd, erase);
663 if (!ret) {
664 set_current_state(TASK_UNINTERRUPTIBLE);
665 add_wait_queue(&waitq, &wait);
666 if (erase->state != MTD_ERASE_DONE &&
667 erase->state != MTD_ERASE_FAILED)
668 schedule();
669 remove_wait_queue(&waitq, &wait);
670 set_current_state(TASK_RUNNING);
671
672 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
673 }
674 kfree(erase);
675 }
676 break;
677 }
678
679 case MEMWRITEOOB:
680 {
681 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700682 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000683
Kevin Cernekee97718542009-04-08 22:53:13 -0700684 /* NOTE: writes return length to buf_user->length */
685 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700687 else
688 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
689 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 case MEMREADOOB:
694 {
695 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700696 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Kevin Cernekee97718542009-04-08 22:53:13 -0700698 /* NOTE: writes return length to buf_user->start */
699 if (copy_from_user(&buf, argp, sizeof(buf)))
700 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700702 ret = mtd_do_readoob(mtd, buf.start, buf.length,
703 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 }
706
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700707 case MEMWRITEOOB64:
708 {
709 struct mtd_oob_buf64 buf;
710 struct mtd_oob_buf64 __user *buf_user = argp;
711
712 if (copy_from_user(&buf, argp, sizeof(buf)))
713 ret = -EFAULT;
714 else
715 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
716 (void __user *)(uintptr_t)buf.usr_ptr,
717 &buf_user->length);
718 break;
719 }
720
721 case MEMREADOOB64:
722 {
723 struct mtd_oob_buf64 buf;
724 struct mtd_oob_buf64 __user *buf_user = argp;
725
726 if (copy_from_user(&buf, argp, sizeof(buf)))
727 ret = -EFAULT;
728 else
729 ret = mtd_do_readoob(mtd, buf.start, buf.length,
730 (void __user *)(uintptr_t)buf.usr_ptr,
731 &buf_user->length);
732 break;
733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 case MEMLOCK:
736 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700737 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Harvey Harrison175428b2008-07-03 23:40:14 -0700739 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EFAULT;
741
742 if (!mtd->lock)
743 ret = -EOPNOTSUPP;
744 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700745 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 }
748
749 case MEMUNLOCK:
750 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700751 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Harvey Harrison175428b2008-07-03 23:40:14 -0700753 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EFAULT;
755
756 if (!mtd->unlock)
757 ret = -EOPNOTSUPP;
758 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700759 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 }
762
Richard Cochran99384242010-06-14 18:10:33 +0200763 case MEMISLOCKED:
764 {
765 struct erase_info_user einfo;
766
767 if (copy_from_user(&einfo, argp, sizeof(einfo)))
768 return -EFAULT;
769
770 if (!mtd->is_locked)
771 ret = -EOPNOTSUPP;
772 else
773 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
774 break;
775 }
776
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200777 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case MEMGETOOBSEL:
779 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200780 struct nand_oobinfo oi;
781
782 if (!mtd->ecclayout)
783 return -EOPNOTSUPP;
784 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
785 return -EINVAL;
786
787 oi.useecc = MTD_NANDECC_AUTOPLACE;
788 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
789 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
790 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200791 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200792
793 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -EFAULT;
795 break;
796 }
797
798 case MEMGETBADBLOCK:
799 {
800 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (copy_from_user(&offs, argp, sizeof(loff_t)))
803 return -EFAULT;
804 if (!mtd->block_isbad)
805 ret = -EOPNOTSUPP;
806 else
807 return mtd->block_isbad(mtd, offs);
808 break;
809 }
810
811 case MEMSETBADBLOCK:
812 {
813 loff_t offs;
814
815 if (copy_from_user(&offs, argp, sizeof(loff_t)))
816 return -EFAULT;
817 if (!mtd->block_markbad)
818 ret = -EOPNOTSUPP;
819 else
820 return mtd->block_markbad(mtd, offs);
821 break;
822 }
823
David Brownell34a82442008-07-30 12:35:05 -0700824#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000825 case OTPSELECT:
826 {
827 int mode;
828 if (copy_from_user(&mode, argp, sizeof(int)))
829 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200830
831 mfi->mode = MTD_MODE_NORMAL;
832
833 ret = otp_select_filemode(mfi, mode);
834
Nicolas Pitre81dba482005-04-01 16:36:15 +0100835 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000836 break;
837 }
838
839 case OTPGETREGIONCOUNT:
840 case OTPGETREGIONINFO:
841 {
842 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
843 if (!buf)
844 return -ENOMEM;
845 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200846 switch (mfi->mode) {
847 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000848 if (mtd->get_fact_prot_info)
849 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
850 break;
851 case MTD_MODE_OTP_USER:
852 if (mtd->get_user_prot_info)
853 ret = mtd->get_user_prot_info(mtd, buf, 4096);
854 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200855 default:
856 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000857 }
858 if (ret >= 0) {
859 if (cmd == OTPGETREGIONCOUNT) {
860 int nbr = ret / sizeof(struct otp_info);
861 ret = copy_to_user(argp, &nbr, sizeof(int));
862 } else
863 ret = copy_to_user(argp, buf, ret);
864 if (ret)
865 ret = -EFAULT;
866 }
867 kfree(buf);
868 break;
869 }
870
871 case OTPLOCK:
872 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700873 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000874
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200875 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000876 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700877 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000878 return -EFAULT;
879 if (!mtd->lock_user_prot_reg)
880 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700881 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000882 break;
883 }
884#endif
885
Brian Norriscc26c3c2010-08-24 18:12:00 -0700886 /* This ioctl is being deprecated - it truncates the ecc layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200887 case ECCGETLAYOUT:
888 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700889 struct nand_ecclayout_user *usrlay;
890
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200891 if (!mtd->ecclayout)
892 return -EOPNOTSUPP;
893
Brian Norriscc26c3c2010-08-24 18:12:00 -0700894 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
895 if (!usrlay)
896 return -ENOMEM;
897
898 shrink_ecclayout(mtd->ecclayout, usrlay);
899
900 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
901 ret = -EFAULT;
902 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200903 break;
904 }
905
906 case ECCGETSTATS:
907 {
Murali Palnati3e8c3bf2012-02-15 16:51:17 +0530908#ifdef CONFIG_MTD_LAZYECCSTATS
909 part_fill_badblockstats(mtd);
910#endif
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200911 if (copy_to_user(argp, &mtd->ecc_stats,
912 sizeof(struct mtd_ecc_stats)))
913 return -EFAULT;
914 break;
915 }
916
917 case MTDFILEMODE:
918 {
919 mfi->mode = 0;
920
921 switch(arg) {
922 case MTD_MODE_OTP_FACTORY:
923 case MTD_MODE_OTP_USER:
924 ret = otp_select_filemode(mfi, arg);
925 break;
926
927 case MTD_MODE_RAW:
928 if (!mtd->read_oob || !mtd->write_oob)
929 return -EOPNOTSUPP;
930 mfi->mode = arg;
931
932 case MTD_MODE_NORMAL:
933 break;
934 default:
935 ret = -EINVAL;
936 }
937 file->f_pos = 0;
938 break;
939 }
940
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300941 case BLKPG:
942 {
943 ret = mtd_blkpg_ioctl(mtd,
944 (struct blkpg_ioctl_arg __user *)arg);
945 break;
946 }
947
948 case BLKRRPART:
949 {
950 /* No reread partition feature. Just return ok */
951 ret = 0;
952 break;
953 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 default:
956 ret = -ENOTTY;
957 }
958
959 return ret;
960} /* memory_ioctl */
961
Arnd Bergmann55929332010-04-27 00:24:05 +0200962static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
963{
964 int ret;
965
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200966 mutex_lock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200967 ret = mtd_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200968 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200969
970 return ret;
971}
972
Kevin Cernekee97718542009-04-08 22:53:13 -0700973#ifdef CONFIG_COMPAT
974
975struct mtd_oob_buf32 {
976 u_int32_t start;
977 u_int32_t length;
978 compat_caddr_t ptr; /* unsigned char* */
979};
980
981#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
982#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
983
984static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
985 unsigned long arg)
986{
987 struct mtd_file_info *mfi = file->private_data;
988 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100989 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700990 int ret = 0;
991
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200992 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -0700993
994 switch (cmd) {
995 case MEMWRITEOOB32:
996 {
997 struct mtd_oob_buf32 buf;
998 struct mtd_oob_buf32 __user *buf_user = argp;
999
1000 if (copy_from_user(&buf, argp, sizeof(buf)))
1001 ret = -EFAULT;
1002 else
1003 ret = mtd_do_writeoob(file, mtd, buf.start,
1004 buf.length, compat_ptr(buf.ptr),
1005 &buf_user->length);
1006 break;
1007 }
1008
1009 case MEMREADOOB32:
1010 {
1011 struct mtd_oob_buf32 buf;
1012 struct mtd_oob_buf32 __user *buf_user = argp;
1013
1014 /* NOTE: writes return length to buf->start */
1015 if (copy_from_user(&buf, argp, sizeof(buf)))
1016 ret = -EFAULT;
1017 else
1018 ret = mtd_do_readoob(mtd, buf.start,
1019 buf.length, compat_ptr(buf.ptr),
1020 &buf_user->start);
1021 break;
1022 }
1023 default:
Arnd Bergmann55929332010-04-27 00:24:05 +02001024 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001025 }
1026
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001027 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001028
1029 return ret;
1030}
1031
1032#endif /* CONFIG_COMPAT */
1033
David Howells402d3262009-02-12 10:40:00 +00001034/*
1035 * try to determine where a shared mapping can be made
1036 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1037 * mappings)
1038 */
1039#ifndef CONFIG_MMU
1040static unsigned long mtd_get_unmapped_area(struct file *file,
1041 unsigned long addr,
1042 unsigned long len,
1043 unsigned long pgoff,
1044 unsigned long flags)
1045{
1046 struct mtd_file_info *mfi = file->private_data;
1047 struct mtd_info *mtd = mfi->mtd;
1048
1049 if (mtd->get_unmapped_area) {
1050 unsigned long offset;
1051
1052 if (addr != 0)
1053 return (unsigned long) -EINVAL;
1054
1055 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1056 return (unsigned long) -EINVAL;
1057
1058 offset = pgoff << PAGE_SHIFT;
1059 if (offset > mtd->size - len)
1060 return (unsigned long) -EINVAL;
1061
1062 return mtd->get_unmapped_area(mtd, len, offset, flags);
1063 }
1064
1065 /* can't map directly */
1066 return (unsigned long) -ENOSYS;
1067}
1068#endif
1069
1070/*
1071 * set up a mapping for shared memory segments
1072 */
1073static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
1074{
1075#ifdef CONFIG_MMU
1076 struct mtd_file_info *mfi = file->private_data;
1077 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001078 struct map_info *map = mtd->priv;
1079 unsigned long start;
1080 unsigned long off;
1081 u32 len;
David Howells402d3262009-02-12 10:40:00 +00001082
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001083 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1084 off = vma->vm_pgoff << PAGE_SHIFT;
1085 start = map->phys;
1086 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1087 start &= PAGE_MASK;
1088 if ((vma->vm_end - vma->vm_start + off) > len)
1089 return -EINVAL;
1090
1091 off += start;
1092 vma->vm_pgoff = off >> PAGE_SHIFT;
1093 vma->vm_flags |= VM_IO | VM_RESERVED;
1094
1095#ifdef pgprot_noncached
1096 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1097 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1098#endif
1099 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1100 vma->vm_end - vma->vm_start,
1101 vma->vm_page_prot))
1102 return -EAGAIN;
1103
David Howells402d3262009-02-12 10:40:00 +00001104 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001105 }
David Howells402d3262009-02-12 10:40:00 +00001106 return -ENOSYS;
1107#else
1108 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1109#endif
1110}
1111
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001112static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 .owner = THIS_MODULE,
1114 .llseek = mtd_lseek,
1115 .read = mtd_read,
1116 .write = mtd_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001117 .unlocked_ioctl = mtd_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001118#ifdef CONFIG_COMPAT
1119 .compat_ioctl = mtd_compat_ioctl,
1120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 .open = mtd_open,
1122 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +00001123 .mmap = mtd_mmap,
1124#ifndef CONFIG_MMU
1125 .get_unmapped_area = mtd_get_unmapped_area,
1126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127};
1128
Al Viro51139ad2010-07-25 23:47:46 +04001129static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1130 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001131{
Al Viroc74a1cb2011-01-12 16:59:34 -05001132 return mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001133}
1134
1135static struct file_system_type mtd_inodefs_type = {
1136 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001137 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001138 .kill_sb = kill_anon_super,
1139};
1140
1141static void mtdchar_notify_add(struct mtd_info *mtd)
1142{
1143}
1144
1145static void mtdchar_notify_remove(struct mtd_info *mtd)
1146{
1147 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1148
1149 if (mtd_ino) {
1150 /* Destroy the inode if it exists */
1151 mtd_ino->i_nlink = 0;
1152 iput(mtd_ino);
1153 }
1154}
1155
1156static struct mtd_notifier mtdchar_notifier = {
1157 .add = mtdchar_notify_add,
1158 .remove = mtdchar_notify_remove,
1159};
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161static int __init init_mtdchar(void)
1162{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001163 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001164
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001165 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001166 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001167 if (ret < 0) {
1168 pr_notice("Can't allocate major number %d for "
1169 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001173 ret = register_filesystem(&mtd_inodefs_type);
1174 if (ret) {
1175 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1176 goto err_unregister_chdev;
1177 }
1178
1179 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1180 if (IS_ERR(mtd_inode_mnt)) {
1181 ret = PTR_ERR(mtd_inode_mnt);
1182 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1183 goto err_unregister_filesystem;
1184 }
1185 register_mtd_user(&mtdchar_notifier);
1186
1187 return ret;
1188
1189err_unregister_filesystem:
1190 unregister_filesystem(&mtd_inodefs_type);
1191err_unregister_chdev:
1192 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196static void __exit cleanup_mtdchar(void)
1197{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001198 unregister_mtd_user(&mtdchar_notifier);
Tim Chen6bc63752011-07-19 09:32:38 -07001199 kern_unmount(mtd_inode_mnt);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001200 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001201 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204module_init(init_mtdchar);
1205module_exit(cleanup_mtdchar);
1206
David Brownell1f24b5a2009-03-26 00:42:41 -07001207MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209MODULE_LICENSE("GPL");
1210MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1211MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001212MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);