blob: 0811d009170dac438b09bc740585211a1886c28c [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;
Peter Wippich5805ad82011-06-06 15:50:58 +0200324 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200325 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
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300513static int mtd_blkpg_ioctl(struct mtd_info *mtd,
514 struct blkpg_ioctl_arg __user *arg)
515{
516 struct blkpg_ioctl_arg a;
517 struct blkpg_partition p;
518
519 if (!capable(CAP_SYS_ADMIN))
520 return -EPERM;
521
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300522 if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
523 return -EFAULT;
524
525 if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
526 return -EFAULT;
527
528 switch (a.op) {
529 case BLKPG_ADD_PARTITION:
530
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200531 /* Only master mtd device must be used to add partitions */
532 if (mtd_is_partition(mtd))
533 return -EINVAL;
534
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300535 return mtd_add_partition(mtd, p.devname, p.start, p.length);
536
537 case BLKPG_DEL_PARTITION:
538
539 if (p.pno < 0)
540 return -EINVAL;
541
542 return mtd_del_partition(mtd, p.pno);
543
544 default:
545 return -EINVAL;
546 }
547}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300548
Arnd Bergmann55929332010-04-27 00:24:05 +0200549static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200551 struct mtd_file_info *mfi = file->private_data;
552 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 void __user *argp = (void __user *)arg;
554 int ret = 0;
555 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200556 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
559
560 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
561 if (cmd & IOC_IN) {
562 if (!access_ok(VERIFY_READ, argp, size))
563 return -EFAULT;
564 }
565 if (cmd & IOC_OUT) {
566 if (!access_ok(VERIFY_WRITE, argp, size))
567 return -EFAULT;
568 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (cmd) {
571 case MEMGETREGIONCOUNT:
572 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
573 return -EFAULT;
574 break;
575
576 case MEMGETREGIONINFO:
577 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700578 uint32_t ur_idx;
579 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700580 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Zev Weissb67c5f82008-09-01 05:02:12 -0700582 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EFAULT;
584
Dan Carpenter5e59be12010-09-08 21:39:56 +0200585 if (ur_idx >= mtd->numeraseregions)
586 return -EINVAL;
587
Zev Weissb67c5f82008-09-01 05:02:12 -0700588 kr = &(mtd->eraseregions[ur_idx]);
589
590 if (put_user(kr->offset, &(ur->offset))
591 || put_user(kr->erasesize, &(ur->erasesize))
592 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 }
597
598 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300599 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200600 info.type = mtd->type;
601 info.flags = mtd->flags;
602 info.size = mtd->size;
603 info.erasesize = mtd->erasesize;
604 info.writesize = mtd->writesize;
605 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200606 /* The below fields are obsolete */
607 info.ecctype = -1;
Joern Engel73c619e2006-05-30 14:25:35 +0200608 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -EFAULT;
610 break;
611
612 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700613 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 {
615 struct erase_info *erase;
616
Al Viroaeb5d722008-09-02 15:28:45 -0400617 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EPERM;
619
Burman Yan95b93a02006-11-15 21:10:29 +0200620 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!erase)
622 ret = -ENOMEM;
623 else {
624 wait_queue_head_t waitq;
625 DECLARE_WAITQUEUE(wait, current);
626
627 init_waitqueue_head(&waitq);
628
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700629 if (cmd == MEMERASE64) {
630 struct erase_info_user64 einfo64;
631
632 if (copy_from_user(&einfo64, argp,
633 sizeof(struct erase_info_user64))) {
634 kfree(erase);
635 return -EFAULT;
636 }
637 erase->addr = einfo64.start;
638 erase->len = einfo64.length;
639 } else {
640 struct erase_info_user einfo32;
641
642 if (copy_from_user(&einfo32, argp,
643 sizeof(struct erase_info_user))) {
644 kfree(erase);
645 return -EFAULT;
646 }
647 erase->addr = einfo32.start;
648 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650 erase->mtd = mtd;
651 erase->callback = mtdchar_erase_callback;
652 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 FIXME: Allow INTERRUPTIBLE. Which means
656 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 If the wq_head is on the stack, and we
659 leave because we got interrupted, then the
660 wq_head is no longer there when the
661 callback routine tries to wake us up.
662 */
663 ret = mtd->erase(mtd, erase);
664 if (!ret) {
665 set_current_state(TASK_UNINTERRUPTIBLE);
666 add_wait_queue(&waitq, &wait);
667 if (erase->state != MTD_ERASE_DONE &&
668 erase->state != MTD_ERASE_FAILED)
669 schedule();
670 remove_wait_queue(&waitq, &wait);
671 set_current_state(TASK_RUNNING);
672
673 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
674 }
675 kfree(erase);
676 }
677 break;
678 }
679
680 case MEMWRITEOOB:
681 {
682 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700683 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000684
Kevin Cernekee97718542009-04-08 22:53:13 -0700685 /* NOTE: writes return length to buf_user->length */
686 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700688 else
689 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
690 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 case MEMREADOOB:
695 {
696 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700697 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Kevin Cernekee97718542009-04-08 22:53:13 -0700699 /* NOTE: writes return length to buf_user->start */
700 if (copy_from_user(&buf, argp, sizeof(buf)))
701 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700703 ret = mtd_do_readoob(mtd, buf.start, buf.length,
704 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 }
707
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700708 case MEMWRITEOOB64:
709 {
710 struct mtd_oob_buf64 buf;
711 struct mtd_oob_buf64 __user *buf_user = argp;
712
713 if (copy_from_user(&buf, argp, sizeof(buf)))
714 ret = -EFAULT;
715 else
716 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
717 (void __user *)(uintptr_t)buf.usr_ptr,
718 &buf_user->length);
719 break;
720 }
721
722 case MEMREADOOB64:
723 {
724 struct mtd_oob_buf64 buf;
725 struct mtd_oob_buf64 __user *buf_user = argp;
726
727 if (copy_from_user(&buf, argp, sizeof(buf)))
728 ret = -EFAULT;
729 else
730 ret = mtd_do_readoob(mtd, buf.start, buf.length,
731 (void __user *)(uintptr_t)buf.usr_ptr,
732 &buf_user->length);
733 break;
734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case MEMLOCK:
737 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700738 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Harvey Harrison175428b2008-07-03 23:40:14 -0700740 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EFAULT;
742
743 if (!mtd->lock)
744 ret = -EOPNOTSUPP;
745 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700746 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 }
749
750 case MEMUNLOCK:
751 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700752 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Harvey Harrison175428b2008-07-03 23:40:14 -0700754 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EFAULT;
756
757 if (!mtd->unlock)
758 ret = -EOPNOTSUPP;
759 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700760 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
763
Richard Cochran99384242010-06-14 18:10:33 +0200764 case MEMISLOCKED:
765 {
766 struct erase_info_user einfo;
767
768 if (copy_from_user(&einfo, argp, sizeof(einfo)))
769 return -EFAULT;
770
771 if (!mtd->is_locked)
772 ret = -EOPNOTSUPP;
773 else
774 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
775 break;
776 }
777
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200778 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 case MEMGETOOBSEL:
780 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200781 struct nand_oobinfo oi;
782
783 if (!mtd->ecclayout)
784 return -EOPNOTSUPP;
785 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
786 return -EINVAL;
787
788 oi.useecc = MTD_NANDECC_AUTOPLACE;
789 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
790 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
791 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200792 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200793
794 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EFAULT;
796 break;
797 }
798
799 case MEMGETBADBLOCK:
800 {
801 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (copy_from_user(&offs, argp, sizeof(loff_t)))
804 return -EFAULT;
805 if (!mtd->block_isbad)
806 ret = -EOPNOTSUPP;
807 else
808 return mtd->block_isbad(mtd, offs);
809 break;
810 }
811
812 case MEMSETBADBLOCK:
813 {
814 loff_t offs;
815
816 if (copy_from_user(&offs, argp, sizeof(loff_t)))
817 return -EFAULT;
818 if (!mtd->block_markbad)
819 ret = -EOPNOTSUPP;
820 else
821 return mtd->block_markbad(mtd, offs);
822 break;
823 }
824
David Brownell34a82442008-07-30 12:35:05 -0700825#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000826 case OTPSELECT:
827 {
828 int mode;
829 if (copy_from_user(&mode, argp, sizeof(int)))
830 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200831
832 mfi->mode = MTD_MODE_NORMAL;
833
834 ret = otp_select_filemode(mfi, mode);
835
Nicolas Pitre81dba482005-04-01 16:36:15 +0100836 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000837 break;
838 }
839
840 case OTPGETREGIONCOUNT:
841 case OTPGETREGIONINFO:
842 {
843 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
844 if (!buf)
845 return -ENOMEM;
846 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200847 switch (mfi->mode) {
848 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000849 if (mtd->get_fact_prot_info)
850 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
851 break;
852 case MTD_MODE_OTP_USER:
853 if (mtd->get_user_prot_info)
854 ret = mtd->get_user_prot_info(mtd, buf, 4096);
855 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200856 default:
857 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000858 }
859 if (ret >= 0) {
860 if (cmd == OTPGETREGIONCOUNT) {
861 int nbr = ret / sizeof(struct otp_info);
862 ret = copy_to_user(argp, &nbr, sizeof(int));
863 } else
864 ret = copy_to_user(argp, buf, ret);
865 if (ret)
866 ret = -EFAULT;
867 }
868 kfree(buf);
869 break;
870 }
871
872 case OTPLOCK:
873 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700874 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000875
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200876 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000877 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700878 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000879 return -EFAULT;
880 if (!mtd->lock_user_prot_reg)
881 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700882 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000883 break;
884 }
885#endif
886
Brian Norriscc26c3c2010-08-24 18:12:00 -0700887 /* This ioctl is being deprecated - it truncates the ecc layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200888 case ECCGETLAYOUT:
889 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700890 struct nand_ecclayout_user *usrlay;
891
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200892 if (!mtd->ecclayout)
893 return -EOPNOTSUPP;
894
Brian Norriscc26c3c2010-08-24 18:12:00 -0700895 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
896 if (!usrlay)
897 return -ENOMEM;
898
899 shrink_ecclayout(mtd->ecclayout, usrlay);
900
901 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
902 ret = -EFAULT;
903 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200904 break;
905 }
906
907 case ECCGETSTATS:
908 {
Murali Palnati3e8c3bf2012-02-15 16:51:17 +0530909#ifdef CONFIG_MTD_LAZYECCSTATS
910 part_fill_badblockstats(mtd);
911#endif
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200912 if (copy_to_user(argp, &mtd->ecc_stats,
913 sizeof(struct mtd_ecc_stats)))
914 return -EFAULT;
915 break;
916 }
917
918 case MTDFILEMODE:
919 {
920 mfi->mode = 0;
921
922 switch(arg) {
923 case MTD_MODE_OTP_FACTORY:
924 case MTD_MODE_OTP_USER:
925 ret = otp_select_filemode(mfi, arg);
926 break;
927
928 case MTD_MODE_RAW:
929 if (!mtd->read_oob || !mtd->write_oob)
930 return -EOPNOTSUPP;
931 mfi->mode = arg;
932
933 case MTD_MODE_NORMAL:
934 break;
935 default:
936 ret = -EINVAL;
937 }
938 file->f_pos = 0;
939 break;
940 }
941
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300942 case BLKPG:
943 {
944 ret = mtd_blkpg_ioctl(mtd,
945 (struct blkpg_ioctl_arg __user *)arg);
946 break;
947 }
948
949 case BLKRRPART:
950 {
951 /* No reread partition feature. Just return ok */
952 ret = 0;
953 break;
954 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 default:
957 ret = -ENOTTY;
958 }
959
960 return ret;
961} /* memory_ioctl */
962
Arnd Bergmann55929332010-04-27 00:24:05 +0200963static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
964{
965 int ret;
966
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200967 mutex_lock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200968 ret = mtd_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200969 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200970
971 return ret;
972}
973
Kevin Cernekee97718542009-04-08 22:53:13 -0700974#ifdef CONFIG_COMPAT
975
976struct mtd_oob_buf32 {
977 u_int32_t start;
978 u_int32_t length;
979 compat_caddr_t ptr; /* unsigned char* */
980};
981
982#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
983#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
984
985static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
986 unsigned long arg)
987{
988 struct mtd_file_info *mfi = file->private_data;
989 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100990 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700991 int ret = 0;
992
Arnd Bergmann5aa82942010-06-02 14:28:52 +0200993 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -0700994
995 switch (cmd) {
996 case MEMWRITEOOB32:
997 {
998 struct mtd_oob_buf32 buf;
999 struct mtd_oob_buf32 __user *buf_user = argp;
1000
1001 if (copy_from_user(&buf, argp, sizeof(buf)))
1002 ret = -EFAULT;
1003 else
1004 ret = mtd_do_writeoob(file, mtd, buf.start,
1005 buf.length, compat_ptr(buf.ptr),
1006 &buf_user->length);
1007 break;
1008 }
1009
1010 case MEMREADOOB32:
1011 {
1012 struct mtd_oob_buf32 buf;
1013 struct mtd_oob_buf32 __user *buf_user = argp;
1014
1015 /* NOTE: writes return length to buf->start */
1016 if (copy_from_user(&buf, argp, sizeof(buf)))
1017 ret = -EFAULT;
1018 else
1019 ret = mtd_do_readoob(mtd, buf.start,
1020 buf.length, compat_ptr(buf.ptr),
1021 &buf_user->start);
1022 break;
1023 }
1024 default:
Arnd Bergmann55929332010-04-27 00:24:05 +02001025 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001026 }
1027
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001028 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001029
1030 return ret;
1031}
1032
1033#endif /* CONFIG_COMPAT */
1034
David Howells402d3262009-02-12 10:40:00 +00001035/*
1036 * try to determine where a shared mapping can be made
1037 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1038 * mappings)
1039 */
1040#ifndef CONFIG_MMU
1041static unsigned long mtd_get_unmapped_area(struct file *file,
1042 unsigned long addr,
1043 unsigned long len,
1044 unsigned long pgoff,
1045 unsigned long flags)
1046{
1047 struct mtd_file_info *mfi = file->private_data;
1048 struct mtd_info *mtd = mfi->mtd;
1049
1050 if (mtd->get_unmapped_area) {
1051 unsigned long offset;
1052
1053 if (addr != 0)
1054 return (unsigned long) -EINVAL;
1055
1056 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1057 return (unsigned long) -EINVAL;
1058
1059 offset = pgoff << PAGE_SHIFT;
1060 if (offset > mtd->size - len)
1061 return (unsigned long) -EINVAL;
1062
1063 return mtd->get_unmapped_area(mtd, len, offset, flags);
1064 }
1065
1066 /* can't map directly */
1067 return (unsigned long) -ENOSYS;
1068}
1069#endif
1070
1071/*
1072 * set up a mapping for shared memory segments
1073 */
1074static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
1075{
1076#ifdef CONFIG_MMU
1077 struct mtd_file_info *mfi = file->private_data;
1078 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001079 struct map_info *map = mtd->priv;
1080 unsigned long start;
1081 unsigned long off;
1082 u32 len;
David Howells402d3262009-02-12 10:40:00 +00001083
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001084 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1085 off = vma->vm_pgoff << PAGE_SHIFT;
1086 start = map->phys;
1087 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1088 start &= PAGE_MASK;
1089 if ((vma->vm_end - vma->vm_start + off) > len)
1090 return -EINVAL;
1091
1092 off += start;
1093 vma->vm_pgoff = off >> PAGE_SHIFT;
1094 vma->vm_flags |= VM_IO | VM_RESERVED;
1095
1096#ifdef pgprot_noncached
1097 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1098 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1099#endif
1100 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1101 vma->vm_end - vma->vm_start,
1102 vma->vm_page_prot))
1103 return -EAGAIN;
1104
David Howells402d3262009-02-12 10:40:00 +00001105 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001106 }
David Howells402d3262009-02-12 10:40:00 +00001107 return -ENOSYS;
1108#else
1109 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1110#endif
1111}
1112
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001113static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 .owner = THIS_MODULE,
1115 .llseek = mtd_lseek,
1116 .read = mtd_read,
1117 .write = mtd_write,
Arnd Bergmann55929332010-04-27 00:24:05 +02001118 .unlocked_ioctl = mtd_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001119#ifdef CONFIG_COMPAT
1120 .compat_ioctl = mtd_compat_ioctl,
1121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 .open = mtd_open,
1123 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +00001124 .mmap = mtd_mmap,
1125#ifndef CONFIG_MMU
1126 .get_unmapped_area = mtd_get_unmapped_area,
1127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128};
1129
Al Viro51139ad2010-07-25 23:47:46 +04001130static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1131 int flags, const char *dev_name, void *data)
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001132{
Al Viroc74a1cb2011-01-12 16:59:34 -05001133 return mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001134}
1135
1136static struct file_system_type mtd_inodefs_type = {
1137 .name = "mtd_inodefs",
Al Viro51139ad2010-07-25 23:47:46 +04001138 .mount = mtd_inodefs_mount,
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001139 .kill_sb = kill_anon_super,
1140};
1141
1142static void mtdchar_notify_add(struct mtd_info *mtd)
1143{
1144}
1145
1146static void mtdchar_notify_remove(struct mtd_info *mtd)
1147{
1148 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1149
1150 if (mtd_ino) {
1151 /* Destroy the inode if it exists */
1152 mtd_ino->i_nlink = 0;
1153 iput(mtd_ino);
1154 }
1155}
1156
1157static struct mtd_notifier mtdchar_notifier = {
1158 .add = mtdchar_notify_add,
1159 .remove = mtdchar_notify_remove,
1160};
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int __init init_mtdchar(void)
1163{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001164 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001165
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001166 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001167 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001168 if (ret < 0) {
1169 pr_notice("Can't allocate major number %d for "
1170 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001174 ret = register_filesystem(&mtd_inodefs_type);
1175 if (ret) {
1176 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1177 goto err_unregister_chdev;
1178 }
1179
1180 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1181 if (IS_ERR(mtd_inode_mnt)) {
1182 ret = PTR_ERR(mtd_inode_mnt);
1183 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1184 goto err_unregister_filesystem;
1185 }
1186 register_mtd_user(&mtdchar_notifier);
1187
1188 return ret;
1189
1190err_unregister_filesystem:
1191 unregister_filesystem(&mtd_inodefs_type);
1192err_unregister_chdev:
1193 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1194 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197static void __exit cleanup_mtdchar(void)
1198{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001199 unregister_mtd_user(&mtdchar_notifier);
Tim Chen6bc63752011-07-19 09:32:38 -07001200 kern_unmount(mtd_inode_mnt);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001201 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001202 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205module_init(init_mtdchar);
1206module_exit(cleanup_mtdchar);
1207
David Brownell1f24b5a2009-03-26 00:42:41 -07001208MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210MODULE_LICENSE("GPL");
1211MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1212MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001213MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);