blob: f6d76f1adca03605ddb2a58c83c8bd0e4635a4b4 [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#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Gleixner15fdc522005-11-07 00:14:42 +010038#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010039
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030040#define MTD_INODE_FS_MAGIC 0x11307854
41static struct vfsmount *mtd_inode_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Nicolas Pitre045e9a52005-02-08 19:12:53 +000043/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020044 * Data structure to hold the pointer to the mtd device as well
45 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000046 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020047struct mtd_file_info {
48 struct mtd_info *mtd;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030049 struct inode *ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020050 enum mtd_file_modes mode;
51};
Nicolas Pitre31f42332005-02-08 17:45:55 +000052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
54{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020055 struct mtd_file_info *mfi = file->private_data;
56 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040059 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040061 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010062 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040064 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010065 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67 default:
68 return -EINVAL;
69 }
70
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020071 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010072 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Todd Poynor8b491d72005-08-04 02:05:51 +010074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77
78
79static int mtd_open(struct inode *inode, struct file *file)
80{
81 int minor = iminor(inode);
82 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060083 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020085 struct mtd_file_info *mfi;
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030086 struct inode *mtd_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040091 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EACCES;
93
Jonathan Corbet60712392008-05-15 10:10:37 -060094 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000096
Jonathan Corbet60712392008-05-15 10:10:37 -060097 if (IS_ERR(mtd)) {
98 ret = PTR_ERR(mtd);
99 goto out;
100 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000101
David Howells402d3262009-02-12 10:40:00 +0000102 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600104 ret = -ENODEV;
105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300108 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
109 if (!mtd_ino) {
110 put_mtd_device(mtd);
111 ret = -ENOMEM;
112 goto out;
113 }
114 if (mtd_ino->i_state & I_NEW) {
115 mtd_ino->i_private = mtd;
116 mtd_ino->i_mode = S_IFCHR;
117 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
118 unlock_new_inode(mtd_ino);
119 }
120 file->f_mapping = mtd_ino->i_mapping;
David Howells402d3262009-02-12 10:40:00 +0000121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -0400123 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300124 iput(mtd_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600126 ret = -EACCES;
127 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000129
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200130 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
131 if (!mfi) {
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300132 iput(mtd_ino);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200133 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600134 ret = -ENOMEM;
135 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200136 }
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300137 mfi->ino = mtd_ino;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200138 mfi->mtd = mtd;
139 file->private_data = mfi;
140
Jonathan Corbet60712392008-05-15 10:10:37 -0600141out:
142 unlock_kernel();
143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144} /* mtd_open */
145
146/*====================================================================*/
147
148static int mtd_close(struct inode *inode, struct file *file)
149{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200150 struct mtd_file_info *mfi = file->private_data;
151 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
154
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200155 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400156 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000158
Kirill A. Shutemovcd874232010-05-17 16:55:47 +0300159 iput(mfi->ino);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200162 file->private_data = NULL;
163 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return 0;
166} /* mtd_close */
167
168/* FIXME: This _really_ needs to die. In 2.5, we should lock the
169 userspace buffer down and use it directly with readv/writev.
170*/
171#define MAX_KMALLOC_SIZE 0x20000
172
173static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
174{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200175 struct mtd_file_info *mfi = file->private_data;
176 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 size_t retlen=0;
178 size_t total_retlen=0;
179 int ret=0;
180 int len;
181 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
184
185 if (*ppos + count > mtd->size)
186 count = mtd->size - *ppos;
187
188 if (!count)
189 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
192 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100193
194 if (count > MAX_KMALLOC_SIZE)
195 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
196 else
197 kbuf=kmalloc(count, GFP_KERNEL);
198
199 if (!kbuf)
200 return -ENOMEM;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100203
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000204 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 len = MAX_KMALLOC_SIZE;
206 else
207 len = count;
208
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200209 switch (mfi->mode) {
210 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000211 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
212 break;
213 case MTD_MODE_OTP_USER:
214 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
215 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200216 case MTD_MODE_RAW:
217 {
218 struct mtd_oob_ops ops;
219
220 ops.mode = MTD_OOB_RAW;
221 ops.datbuf = kbuf;
222 ops.oobbuf = NULL;
223 ops.len = len;
224
225 ret = mtd->read_oob(mtd, *ppos, &ops);
226 retlen = ops.retlen;
227 break;
228 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000229 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200230 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Nand returns -EBADMSG on ecc errors, but it returns
233 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000234 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200235 * For kernel internal usage it also might return -EUCLEAN
236 * to signal the caller that a bitflip has occured and has
237 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * Userspace software which accesses NAND this way
239 * must be aware of the fact that it deals with NAND
240 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200241 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *ppos += retlen;
243 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200244 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EFAULT;
246 }
247 else
248 total_retlen += retlen;
249
250 count -= retlen;
251 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000252 if (retlen == 0)
253 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 else {
256 kfree(kbuf);
257 return ret;
258 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Thago Galesib802c072006-04-17 17:38:15 +0100262 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return total_retlen;
264} /* mtd_read */
265
266static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
267{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200268 struct mtd_file_info *mfi = file->private_data;
269 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 char *kbuf;
271 size_t retlen;
272 size_t total_retlen=0;
273 int ret=0;
274 int len;
275
276 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (*ppos == mtd->size)
279 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (*ppos + count > mtd->size)
282 count = mtd->size - *ppos;
283
284 if (!count)
285 return 0;
286
Thago Galesib802c072006-04-17 17:38:15 +0100287 if (count > MAX_KMALLOC_SIZE)
288 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
289 else
290 kbuf=kmalloc(count, GFP_KERNEL);
291
292 if (!kbuf)
293 return -ENOMEM;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100296
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000297 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 len = MAX_KMALLOC_SIZE;
299 else
300 len = count;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (copy_from_user(kbuf, buf, len)) {
303 kfree(kbuf);
304 return -EFAULT;
305 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000306
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200307 switch (mfi->mode) {
308 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000309 ret = -EROFS;
310 break;
311 case MTD_MODE_OTP_USER:
312 if (!mtd->write_user_prot_reg) {
313 ret = -EOPNOTSUPP;
314 break;
315 }
316 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
317 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200318
319 case MTD_MODE_RAW:
320 {
321 struct mtd_oob_ops ops;
322
323 ops.mode = MTD_OOB_RAW;
324 ops.datbuf = kbuf;
325 ops.oobbuf = NULL;
326 ops.len = len;
327
328 ret = mtd->write_oob(mtd, *ppos, &ops);
329 retlen = ops.retlen;
330 break;
331 }
332
Nicolas Pitre31f42332005-02-08 17:45:55 +0000333 default:
334 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!ret) {
337 *ppos += retlen;
338 total_retlen += retlen;
339 count -= retlen;
340 buf += retlen;
341 }
342 else {
343 kfree(kbuf);
344 return ret;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
Thago Galesib802c072006-04-17 17:38:15 +0100348 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return total_retlen;
350} /* mtd_write */
351
352/*======================================================================
353
354 IOCTL calls for getting device parameters.
355
356======================================================================*/
357static void mtdchar_erase_callback (struct erase_info *instr)
358{
359 wake_up((wait_queue_head_t *)instr->priv);
360}
361
David Brownell34a82442008-07-30 12:35:05 -0700362#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200363static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
364{
365 struct mtd_info *mtd = mfi->mtd;
366 int ret = 0;
367
368 switch (mode) {
369 case MTD_OTP_FACTORY:
370 if (!mtd->read_fact_prot_reg)
371 ret = -EOPNOTSUPP;
372 else
373 mfi->mode = MTD_MODE_OTP_FACTORY;
374 break;
375 case MTD_OTP_USER:
376 if (!mtd->read_fact_prot_reg)
377 ret = -EOPNOTSUPP;
378 else
379 mfi->mode = MTD_MODE_OTP_USER;
380 break;
381 default:
382 ret = -EINVAL;
383 case MTD_OTP_OFF:
384 break;
385 }
386 return ret;
387}
388#else
389# define otp_select_filemode(f,m) -EOPNOTSUPP
390#endif
391
Kevin Cernekee97718542009-04-08 22:53:13 -0700392static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
393 uint64_t start, uint32_t length, void __user *ptr,
394 uint32_t __user *retp)
395{
396 struct mtd_oob_ops ops;
397 uint32_t retlen;
398 int ret = 0;
399
400 if (!(file->f_mode & FMODE_WRITE))
401 return -EPERM;
402
403 if (length > 4096)
404 return -EINVAL;
405
406 if (!mtd->write_oob)
407 ret = -EOPNOTSUPP;
408 else
Roel Kluin00404762010-01-29 10:35:04 +0100409 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700410
411 if (ret)
412 return ret;
413
414 ops.ooblen = length;
415 ops.ooboffs = start & (mtd->oobsize - 1);
416 ops.datbuf = NULL;
417 ops.mode = MTD_OOB_PLACE;
418
419 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
420 return -EINVAL;
421
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200422 ops.oobbuf = memdup_user(ptr, length);
423 if (IS_ERR(ops.oobbuf))
424 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700425
426 start &= ~((uint64_t)mtd->oobsize - 1);
427 ret = mtd->write_oob(mtd, start, &ops);
428
429 if (ops.oobretlen > 0xFFFFFFFFU)
430 ret = -EOVERFLOW;
431 retlen = ops.oobretlen;
432 if (copy_to_user(retp, &retlen, sizeof(length)))
433 ret = -EFAULT;
434
435 kfree(ops.oobbuf);
436 return ret;
437}
438
439static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
440 uint32_t length, void __user *ptr, uint32_t __user *retp)
441{
442 struct mtd_oob_ops ops;
443 int ret = 0;
444
445 if (length > 4096)
446 return -EINVAL;
447
448 if (!mtd->read_oob)
449 ret = -EOPNOTSUPP;
450 else
451 ret = access_ok(VERIFY_WRITE, ptr,
452 length) ? 0 : -EFAULT;
453 if (ret)
454 return ret;
455
456 ops.ooblen = length;
457 ops.ooboffs = start & (mtd->oobsize - 1);
458 ops.datbuf = NULL;
459 ops.mode = MTD_OOB_PLACE;
460
461 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
462 return -EINVAL;
463
464 ops.oobbuf = kmalloc(length, GFP_KERNEL);
465 if (!ops.oobbuf)
466 return -ENOMEM;
467
468 start &= ~((uint64_t)mtd->oobsize - 1);
469 ret = mtd->read_oob(mtd, start, &ops);
470
471 if (put_user(ops.oobretlen, retp))
472 ret = -EFAULT;
473 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
474 ops.oobretlen))
475 ret = -EFAULT;
476
477 kfree(ops.oobbuf);
478 return ret;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int mtd_ioctl(struct inode *inode, struct file *file,
482 u_int cmd, u_long arg)
483{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200484 struct mtd_file_info *mfi = file->private_data;
485 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 void __user *argp = (void __user *)arg;
487 int ret = 0;
488 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200489 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
492
493 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
494 if (cmd & IOC_IN) {
495 if (!access_ok(VERIFY_READ, argp, size))
496 return -EFAULT;
497 }
498 if (cmd & IOC_OUT) {
499 if (!access_ok(VERIFY_WRITE, argp, size))
500 return -EFAULT;
501 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 switch (cmd) {
504 case MEMGETREGIONCOUNT:
505 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
506 return -EFAULT;
507 break;
508
509 case MEMGETREGIONINFO:
510 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700511 uint32_t ur_idx;
512 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700513 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Zev Weissb67c5f82008-09-01 05:02:12 -0700515 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EFAULT;
517
Zev Weissb67c5f82008-09-01 05:02:12 -0700518 kr = &(mtd->eraseregions[ur_idx]);
519
520 if (put_user(kr->offset, &(ur->offset))
521 || put_user(kr->erasesize, &(ur->erasesize))
522 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526 }
527
528 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200529 info.type = mtd->type;
530 info.flags = mtd->flags;
531 info.size = mtd->size;
532 info.erasesize = mtd->erasesize;
533 info.writesize = mtd->writesize;
534 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200535 /* The below fields are obsolete */
536 info.ecctype = -1;
537 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200538 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EFAULT;
540 break;
541
542 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700543 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 {
545 struct erase_info *erase;
546
Al Viroaeb5d722008-09-02 15:28:45 -0400547 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EPERM;
549
Burman Yan95b93a02006-11-15 21:10:29 +0200550 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!erase)
552 ret = -ENOMEM;
553 else {
554 wait_queue_head_t waitq;
555 DECLARE_WAITQUEUE(wait, current);
556
557 init_waitqueue_head(&waitq);
558
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700559 if (cmd == MEMERASE64) {
560 struct erase_info_user64 einfo64;
561
562 if (copy_from_user(&einfo64, argp,
563 sizeof(struct erase_info_user64))) {
564 kfree(erase);
565 return -EFAULT;
566 }
567 erase->addr = einfo64.start;
568 erase->len = einfo64.length;
569 } else {
570 struct erase_info_user einfo32;
571
572 if (copy_from_user(&einfo32, argp,
573 sizeof(struct erase_info_user))) {
574 kfree(erase);
575 return -EFAULT;
576 }
577 erase->addr = einfo32.start;
578 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 erase->mtd = mtd;
581 erase->callback = mtdchar_erase_callback;
582 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 FIXME: Allow INTERRUPTIBLE. Which means
586 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 If the wq_head is on the stack, and we
589 leave because we got interrupted, then the
590 wq_head is no longer there when the
591 callback routine tries to wake us up.
592 */
593 ret = mtd->erase(mtd, erase);
594 if (!ret) {
595 set_current_state(TASK_UNINTERRUPTIBLE);
596 add_wait_queue(&waitq, &wait);
597 if (erase->state != MTD_ERASE_DONE &&
598 erase->state != MTD_ERASE_FAILED)
599 schedule();
600 remove_wait_queue(&waitq, &wait);
601 set_current_state(TASK_RUNNING);
602
603 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
604 }
605 kfree(erase);
606 }
607 break;
608 }
609
610 case MEMWRITEOOB:
611 {
612 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700613 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000614
Kevin Cernekee97718542009-04-08 22:53:13 -0700615 /* NOTE: writes return length to buf_user->length */
616 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700618 else
619 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
620 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 case MEMREADOOB:
625 {
626 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700627 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Kevin Cernekee97718542009-04-08 22:53:13 -0700629 /* NOTE: writes return length to buf_user->start */
630 if (copy_from_user(&buf, argp, sizeof(buf)))
631 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700633 ret = mtd_do_readoob(mtd, buf.start, buf.length,
634 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
636 }
637
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700638 case MEMWRITEOOB64:
639 {
640 struct mtd_oob_buf64 buf;
641 struct mtd_oob_buf64 __user *buf_user = argp;
642
643 if (copy_from_user(&buf, argp, sizeof(buf)))
644 ret = -EFAULT;
645 else
646 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
647 (void __user *)(uintptr_t)buf.usr_ptr,
648 &buf_user->length);
649 break;
650 }
651
652 case MEMREADOOB64:
653 {
654 struct mtd_oob_buf64 buf;
655 struct mtd_oob_buf64 __user *buf_user = argp;
656
657 if (copy_from_user(&buf, argp, sizeof(buf)))
658 ret = -EFAULT;
659 else
660 ret = mtd_do_readoob(mtd, buf.start, buf.length,
661 (void __user *)(uintptr_t)buf.usr_ptr,
662 &buf_user->length);
663 break;
664 }
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case MEMLOCK:
667 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700668 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Harvey Harrison175428b2008-07-03 23:40:14 -0700670 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EFAULT;
672
673 if (!mtd->lock)
674 ret = -EOPNOTSUPP;
675 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700676 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 }
679
680 case MEMUNLOCK:
681 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700682 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Harvey Harrison175428b2008-07-03 23:40:14 -0700684 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -EFAULT;
686
687 if (!mtd->unlock)
688 ret = -EOPNOTSUPP;
689 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700690 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
692 }
693
Richard Cochran99384242010-06-14 18:10:33 +0200694 case MEMISLOCKED:
695 {
696 struct erase_info_user einfo;
697
698 if (copy_from_user(&einfo, argp, sizeof(einfo)))
699 return -EFAULT;
700
701 if (!mtd->is_locked)
702 ret = -EOPNOTSUPP;
703 else
704 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
705 break;
706 }
707
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200708 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 case MEMGETOOBSEL:
710 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200711 struct nand_oobinfo oi;
712
713 if (!mtd->ecclayout)
714 return -EOPNOTSUPP;
715 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
716 return -EINVAL;
717
718 oi.useecc = MTD_NANDECC_AUTOPLACE;
719 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
720 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
721 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200722 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200723
724 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EFAULT;
726 break;
727 }
728
729 case MEMGETBADBLOCK:
730 {
731 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (copy_from_user(&offs, argp, sizeof(loff_t)))
734 return -EFAULT;
735 if (!mtd->block_isbad)
736 ret = -EOPNOTSUPP;
737 else
738 return mtd->block_isbad(mtd, offs);
739 break;
740 }
741
742 case MEMSETBADBLOCK:
743 {
744 loff_t offs;
745
746 if (copy_from_user(&offs, argp, sizeof(loff_t)))
747 return -EFAULT;
748 if (!mtd->block_markbad)
749 ret = -EOPNOTSUPP;
750 else
751 return mtd->block_markbad(mtd, offs);
752 break;
753 }
754
David Brownell34a82442008-07-30 12:35:05 -0700755#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000756 case OTPSELECT:
757 {
758 int mode;
759 if (copy_from_user(&mode, argp, sizeof(int)))
760 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200761
762 mfi->mode = MTD_MODE_NORMAL;
763
764 ret = otp_select_filemode(mfi, mode);
765
Nicolas Pitre81dba482005-04-01 16:36:15 +0100766 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000767 break;
768 }
769
770 case OTPGETREGIONCOUNT:
771 case OTPGETREGIONINFO:
772 {
773 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
774 if (!buf)
775 return -ENOMEM;
776 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200777 switch (mfi->mode) {
778 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000779 if (mtd->get_fact_prot_info)
780 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
781 break;
782 case MTD_MODE_OTP_USER:
783 if (mtd->get_user_prot_info)
784 ret = mtd->get_user_prot_info(mtd, buf, 4096);
785 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200786 default:
787 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000788 }
789 if (ret >= 0) {
790 if (cmd == OTPGETREGIONCOUNT) {
791 int nbr = ret / sizeof(struct otp_info);
792 ret = copy_to_user(argp, &nbr, sizeof(int));
793 } else
794 ret = copy_to_user(argp, buf, ret);
795 if (ret)
796 ret = -EFAULT;
797 }
798 kfree(buf);
799 break;
800 }
801
802 case OTPLOCK:
803 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700804 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000805
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200806 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000807 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700808 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000809 return -EFAULT;
810 if (!mtd->lock_user_prot_reg)
811 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700812 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000813 break;
814 }
815#endif
816
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200817 case ECCGETLAYOUT:
818 {
819 if (!mtd->ecclayout)
820 return -EOPNOTSUPP;
821
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200822 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200823 sizeof(struct nand_ecclayout)))
824 return -EFAULT;
825 break;
826 }
827
828 case ECCGETSTATS:
829 {
830 if (copy_to_user(argp, &mtd->ecc_stats,
831 sizeof(struct mtd_ecc_stats)))
832 return -EFAULT;
833 break;
834 }
835
836 case MTDFILEMODE:
837 {
838 mfi->mode = 0;
839
840 switch(arg) {
841 case MTD_MODE_OTP_FACTORY:
842 case MTD_MODE_OTP_USER:
843 ret = otp_select_filemode(mfi, arg);
844 break;
845
846 case MTD_MODE_RAW:
847 if (!mtd->read_oob || !mtd->write_oob)
848 return -EOPNOTSUPP;
849 mfi->mode = arg;
850
851 case MTD_MODE_NORMAL:
852 break;
853 default:
854 ret = -EINVAL;
855 }
856 file->f_pos = 0;
857 break;
858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 default:
861 ret = -ENOTTY;
862 }
863
864 return ret;
865} /* memory_ioctl */
866
Kevin Cernekee97718542009-04-08 22:53:13 -0700867#ifdef CONFIG_COMPAT
868
869struct mtd_oob_buf32 {
870 u_int32_t start;
871 u_int32_t length;
872 compat_caddr_t ptr; /* unsigned char* */
873};
874
875#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
876#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
877
878static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
879 unsigned long arg)
880{
Kevin Cernekee668ff9a2009-04-14 21:59:22 -0700881 struct inode *inode = file->f_path.dentry->d_inode;
Kevin Cernekee97718542009-04-08 22:53:13 -0700882 struct mtd_file_info *mfi = file->private_data;
883 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100884 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700885 int ret = 0;
886
887 lock_kernel();
888
889 switch (cmd) {
890 case MEMWRITEOOB32:
891 {
892 struct mtd_oob_buf32 buf;
893 struct mtd_oob_buf32 __user *buf_user = argp;
894
895 if (copy_from_user(&buf, argp, sizeof(buf)))
896 ret = -EFAULT;
897 else
898 ret = mtd_do_writeoob(file, mtd, buf.start,
899 buf.length, compat_ptr(buf.ptr),
900 &buf_user->length);
901 break;
902 }
903
904 case MEMREADOOB32:
905 {
906 struct mtd_oob_buf32 buf;
907 struct mtd_oob_buf32 __user *buf_user = argp;
908
909 /* NOTE: writes return length to buf->start */
910 if (copy_from_user(&buf, argp, sizeof(buf)))
911 ret = -EFAULT;
912 else
913 ret = mtd_do_readoob(mtd, buf.start,
914 buf.length, compat_ptr(buf.ptr),
915 &buf_user->start);
916 break;
917 }
918 default:
David Woodhouse0b6585c2009-05-29 16:09:08 +0100919 ret = mtd_ioctl(inode, file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -0700920 }
921
922 unlock_kernel();
923
924 return ret;
925}
926
927#endif /* CONFIG_COMPAT */
928
David Howells402d3262009-02-12 10:40:00 +0000929/*
930 * try to determine where a shared mapping can be made
931 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
932 * mappings)
933 */
934#ifndef CONFIG_MMU
935static unsigned long mtd_get_unmapped_area(struct file *file,
936 unsigned long addr,
937 unsigned long len,
938 unsigned long pgoff,
939 unsigned long flags)
940{
941 struct mtd_file_info *mfi = file->private_data;
942 struct mtd_info *mtd = mfi->mtd;
943
944 if (mtd->get_unmapped_area) {
945 unsigned long offset;
946
947 if (addr != 0)
948 return (unsigned long) -EINVAL;
949
950 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
951 return (unsigned long) -EINVAL;
952
953 offset = pgoff << PAGE_SHIFT;
954 if (offset > mtd->size - len)
955 return (unsigned long) -EINVAL;
956
957 return mtd->get_unmapped_area(mtd, len, offset, flags);
958 }
959
960 /* can't map directly */
961 return (unsigned long) -ENOSYS;
962}
963#endif
964
965/*
966 * set up a mapping for shared memory segments
967 */
968static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
969{
970#ifdef CONFIG_MMU
971 struct mtd_file_info *mfi = file->private_data;
972 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +0200973 struct map_info *map = mtd->priv;
974 unsigned long start;
975 unsigned long off;
976 u32 len;
David Howells402d3262009-02-12 10:40:00 +0000977
Anatolij Gustschindd02b672010-06-15 09:30:15 +0200978 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
979 off = vma->vm_pgoff << PAGE_SHIFT;
980 start = map->phys;
981 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
982 start &= PAGE_MASK;
983 if ((vma->vm_end - vma->vm_start + off) > len)
984 return -EINVAL;
985
986 off += start;
987 vma->vm_pgoff = off >> PAGE_SHIFT;
988 vma->vm_flags |= VM_IO | VM_RESERVED;
989
990#ifdef pgprot_noncached
991 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
992 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
993#endif
994 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
995 vma->vm_end - vma->vm_start,
996 vma->vm_page_prot))
997 return -EAGAIN;
998
David Howells402d3262009-02-12 10:40:00 +0000999 return 0;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001000 }
David Howells402d3262009-02-12 10:40:00 +00001001 return -ENOSYS;
1002#else
1003 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1004#endif
1005}
1006
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001007static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .owner = THIS_MODULE,
1009 .llseek = mtd_lseek,
1010 .read = mtd_read,
1011 .write = mtd_write,
1012 .ioctl = mtd_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001013#ifdef CONFIG_COMPAT
1014 .compat_ioctl = mtd_compat_ioctl,
1015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 .open = mtd_open,
1017 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +00001018 .mmap = mtd_mmap,
1019#ifndef CONFIG_MMU
1020 .get_unmapped_area = mtd_get_unmapped_area,
1021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022};
1023
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001024static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
1025 const char *dev_name, void *data,
1026 struct vfsmount *mnt)
1027{
1028 return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
1029 mnt);
1030}
1031
1032static struct file_system_type mtd_inodefs_type = {
1033 .name = "mtd_inodefs",
1034 .get_sb = mtd_inodefs_get_sb,
1035 .kill_sb = kill_anon_super,
1036};
1037
1038static void mtdchar_notify_add(struct mtd_info *mtd)
1039{
1040}
1041
1042static void mtdchar_notify_remove(struct mtd_info *mtd)
1043{
1044 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1045
1046 if (mtd_ino) {
1047 /* Destroy the inode if it exists */
1048 mtd_ino->i_nlink = 0;
1049 iput(mtd_ino);
1050 }
1051}
1052
1053static struct mtd_notifier mtdchar_notifier = {
1054 .add = mtdchar_notify_add,
1055 .remove = mtdchar_notify_remove,
1056};
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058static int __init init_mtdchar(void)
1059{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001060 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001061
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001062 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001063 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001064 if (ret < 0) {
1065 pr_notice("Can't allocate major number %d for "
1066 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001070 ret = register_filesystem(&mtd_inodefs_type);
1071 if (ret) {
1072 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1073 goto err_unregister_chdev;
1074 }
1075
1076 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1077 if (IS_ERR(mtd_inode_mnt)) {
1078 ret = PTR_ERR(mtd_inode_mnt);
1079 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1080 goto err_unregister_filesystem;
1081 }
1082 register_mtd_user(&mtdchar_notifier);
1083
1084 return ret;
1085
1086err_unregister_filesystem:
1087 unregister_filesystem(&mtd_inodefs_type);
1088err_unregister_chdev:
1089 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1090 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static void __exit cleanup_mtdchar(void)
1094{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001095 unregister_mtd_user(&mtdchar_notifier);
1096 mntput(mtd_inode_mnt);
1097 unregister_filesystem(&mtd_inodefs_type);
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001098 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101module_init(init_mtdchar);
1102module_exit(cleanup_mtdchar);
1103
David Brownell1f24b5a2009-03-26 00:42:41 -07001104MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106MODULE_LICENSE("GPL");
1107MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1108MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +00001109MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);