GNU Linux-libre 4.4.285-gnu1
[releases.git] / drivers / mtd / mtdchar.c
1 /*
2  * 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
17  *
18  */
19
20 #include <linux/device.h>
21 #include <linux/fs.h>
22 #include <linux/mm.h>
23 #include <linux/err.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/mutex.h>
30 #include <linux/backing-dev.h>
31 #include <linux/compat.h>
32 #include <linux/mount.h>
33 #include <linux/blkpg.h>
34 #include <linux/magic.h>
35 #include <linux/major.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.h>
38 #include <linux/mtd/map.h>
39
40 #include <asm/uaccess.h>
41
42 #include "mtdcore.h"
43
44 static DEFINE_MUTEX(mtd_mutex);
45
46 /*
47  * Data structure to hold the pointer to the mtd device as well
48  * as mode information of various use cases.
49  */
50 struct mtd_file_info {
51         struct mtd_info *mtd;
52         enum mtd_file_modes mode;
53 };
54
55 static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
56 {
57         struct mtd_file_info *mfi = file->private_data;
58         return fixed_size_llseek(file, offset, orig, mfi->mtd->size);
59 }
60
61 static int mtdchar_open(struct inode *inode, struct file *file)
62 {
63         int minor = iminor(inode);
64         int devnum = minor >> 1;
65         int ret = 0;
66         struct mtd_info *mtd;
67         struct mtd_file_info *mfi;
68
69         pr_debug("MTD_open\n");
70
71         /* You can't open the RO devices RW */
72         if ((file->f_mode & FMODE_WRITE) && (minor & 1))
73                 return -EACCES;
74
75         mutex_lock(&mtd_mutex);
76         mtd = get_mtd_device(NULL, devnum);
77
78         if (IS_ERR(mtd)) {
79                 ret = PTR_ERR(mtd);
80                 goto out;
81         }
82
83         if (mtd->type == MTD_ABSENT) {
84                 ret = -ENODEV;
85                 goto out1;
86         }
87
88         /* You can't open it RW if it's not a writeable device */
89         if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
90                 ret = -EACCES;
91                 goto out1;
92         }
93
94         mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
95         if (!mfi) {
96                 ret = -ENOMEM;
97                 goto out1;
98         }
99         mfi->mtd = mtd;
100         file->private_data = mfi;
101         mutex_unlock(&mtd_mutex);
102         return 0;
103
104 out1:
105         put_mtd_device(mtd);
106 out:
107         mutex_unlock(&mtd_mutex);
108         return ret;
109 } /* mtdchar_open */
110
111 /*====================================================================*/
112
113 static int mtdchar_close(struct inode *inode, struct file *file)
114 {
115         struct mtd_file_info *mfi = file->private_data;
116         struct mtd_info *mtd = mfi->mtd;
117
118         pr_debug("MTD_close\n");
119
120         /* Only sync if opened RW */
121         if ((file->f_mode & FMODE_WRITE))
122                 mtd_sync(mtd);
123
124         put_mtd_device(mtd);
125         file->private_data = NULL;
126         kfree(mfi);
127
128         return 0;
129 } /* mtdchar_close */
130
131 /* Back in June 2001, dwmw2 wrote:
132  *
133  *   FIXME: This _really_ needs to die. In 2.5, we should lock the
134  *   userspace buffer down and use it directly with readv/writev.
135  *
136  * The implementation below, using mtd_kmalloc_up_to, mitigates
137  * allocation failures when the system is under low-memory situations
138  * or if memory is highly fragmented at the cost of reducing the
139  * performance of the requested transfer due to a smaller buffer size.
140  *
141  * A more complex but more memory-efficient implementation based on
142  * get_user_pages and iovecs to cover extents of those pages is a
143  * longer-term goal, as intimated by dwmw2 above. However, for the
144  * write case, this requires yet more complex head and tail transfer
145  * handling when those head and tail offsets and sizes are such that
146  * alignment requirements are not met in the NAND subdriver.
147  */
148
149 static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
150                         loff_t *ppos)
151 {
152         struct mtd_file_info *mfi = file->private_data;
153         struct mtd_info *mtd = mfi->mtd;
154         size_t retlen;
155         size_t total_retlen=0;
156         int ret=0;
157         int len;
158         size_t size = count;
159         char *kbuf;
160
161         pr_debug("MTD_read\n");
162
163         if (*ppos + count > mtd->size) {
164                 if (*ppos < mtd->size)
165                         count = mtd->size - *ppos;
166                 else
167                         count = 0;
168         }
169
170         if (!count)
171                 return 0;
172
173         kbuf = mtd_kmalloc_up_to(mtd, &size);
174         if (!kbuf)
175                 return -ENOMEM;
176
177         while (count) {
178                 len = min_t(size_t, count, size);
179
180                 switch (mfi->mode) {
181                 case MTD_FILE_MODE_OTP_FACTORY:
182                         ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
183                                                      &retlen, kbuf);
184                         break;
185                 case MTD_FILE_MODE_OTP_USER:
186                         ret = mtd_read_user_prot_reg(mtd, *ppos, len,
187                                                      &retlen, kbuf);
188                         break;
189                 case MTD_FILE_MODE_RAW:
190                 {
191                         struct mtd_oob_ops ops;
192
193                         ops.mode = MTD_OPS_RAW;
194                         ops.datbuf = kbuf;
195                         ops.oobbuf = NULL;
196                         ops.len = len;
197
198                         ret = mtd_read_oob(mtd, *ppos, &ops);
199                         retlen = ops.retlen;
200                         break;
201                 }
202                 default:
203                         ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
204                 }
205                 /* Nand returns -EBADMSG on ECC errors, but it returns
206                  * the data. For our userspace tools it is important
207                  * to dump areas with ECC errors!
208                  * For kernel internal usage it also might return -EUCLEAN
209                  * to signal the caller that a bitflip has occurred and has
210                  * been corrected by the ECC algorithm.
211                  * Userspace software which accesses NAND this way
212                  * must be aware of the fact that it deals with NAND
213                  */
214                 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
215                         *ppos += retlen;
216                         if (copy_to_user(buf, kbuf, retlen)) {
217                                 kfree(kbuf);
218                                 return -EFAULT;
219                         }
220                         else
221                                 total_retlen += retlen;
222
223                         count -= retlen;
224                         buf += retlen;
225                         if (retlen == 0)
226                                 count = 0;
227                 }
228                 else {
229                         kfree(kbuf);
230                         return ret;
231                 }
232
233         }
234
235         kfree(kbuf);
236         return total_retlen;
237 } /* mtdchar_read */
238
239 static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
240                         loff_t *ppos)
241 {
242         struct mtd_file_info *mfi = file->private_data;
243         struct mtd_info *mtd = mfi->mtd;
244         size_t size = count;
245         char *kbuf;
246         size_t retlen;
247         size_t total_retlen=0;
248         int ret=0;
249         int len;
250
251         pr_debug("MTD_write\n");
252
253         if (*ppos >= mtd->size)
254                 return -ENOSPC;
255
256         if (*ppos + count > mtd->size)
257                 count = mtd->size - *ppos;
258
259         if (!count)
260                 return 0;
261
262         kbuf = mtd_kmalloc_up_to(mtd, &size);
263         if (!kbuf)
264                 return -ENOMEM;
265
266         while (count) {
267                 len = min_t(size_t, count, size);
268
269                 if (copy_from_user(kbuf, buf, len)) {
270                         kfree(kbuf);
271                         return -EFAULT;
272                 }
273
274                 switch (mfi->mode) {
275                 case MTD_FILE_MODE_OTP_FACTORY:
276                         ret = -EROFS;
277                         break;
278                 case MTD_FILE_MODE_OTP_USER:
279                         ret = mtd_write_user_prot_reg(mtd, *ppos, len,
280                                                       &retlen, kbuf);
281                         break;
282
283                 case MTD_FILE_MODE_RAW:
284                 {
285                         struct mtd_oob_ops ops;
286
287                         ops.mode = MTD_OPS_RAW;
288                         ops.datbuf = kbuf;
289                         ops.oobbuf = NULL;
290                         ops.ooboffs = 0;
291                         ops.len = len;
292
293                         ret = mtd_write_oob(mtd, *ppos, &ops);
294                         retlen = ops.retlen;
295                         break;
296                 }
297
298                 default:
299                         ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
300                 }
301
302                 /*
303                  * Return -ENOSPC only if no data could be written at all.
304                  * Otherwise just return the number of bytes that actually
305                  * have been written.
306                  */
307                 if ((ret == -ENOSPC) && (total_retlen))
308                         break;
309
310                 if (!ret) {
311                         *ppos += retlen;
312                         total_retlen += retlen;
313                         count -= retlen;
314                         buf += retlen;
315                 }
316                 else {
317                         kfree(kbuf);
318                         return ret;
319                 }
320         }
321
322         kfree(kbuf);
323         return total_retlen;
324 } /* mtdchar_write */
325
326 /*======================================================================
327
328     IOCTL calls for getting device parameters.
329
330 ======================================================================*/
331 static void mtdchar_erase_callback (struct erase_info *instr)
332 {
333         wake_up((wait_queue_head_t *)instr->priv);
334 }
335
336 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
337 {
338         struct mtd_info *mtd = mfi->mtd;
339         size_t retlen;
340
341         switch (mode) {
342         case MTD_OTP_FACTORY:
343                 if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
344                                 -EOPNOTSUPP)
345                         return -EOPNOTSUPP;
346
347                 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
348                 break;
349         case MTD_OTP_USER:
350                 if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
351                                 -EOPNOTSUPP)
352                         return -EOPNOTSUPP;
353
354                 mfi->mode = MTD_FILE_MODE_OTP_USER;
355                 break;
356         case MTD_OTP_OFF:
357                 mfi->mode = MTD_FILE_MODE_NORMAL;
358                 break;
359         default:
360                 return -EINVAL;
361         }
362
363         return 0;
364 }
365
366 static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
367         uint64_t start, uint32_t length, void __user *ptr,
368         uint32_t __user *retp)
369 {
370         struct mtd_file_info *mfi = file->private_data;
371         struct mtd_oob_ops ops;
372         uint32_t retlen;
373         int ret = 0;
374
375         if (length > 4096)
376                 return -EINVAL;
377
378         if (!mtd->_write_oob)
379                 ret = -EOPNOTSUPP;
380         else
381                 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
382
383         if (ret)
384                 return ret;
385
386         ops.ooblen = length;
387         ops.ooboffs = start & (mtd->writesize - 1);
388         ops.datbuf = NULL;
389         ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
390                 MTD_OPS_PLACE_OOB;
391
392         if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
393                 return -EINVAL;
394
395         ops.oobbuf = memdup_user(ptr, length);
396         if (IS_ERR(ops.oobbuf))
397                 return PTR_ERR(ops.oobbuf);
398
399         start &= ~((uint64_t)mtd->writesize - 1);
400         ret = mtd_write_oob(mtd, start, &ops);
401
402         if (ops.oobretlen > 0xFFFFFFFFU)
403                 ret = -EOVERFLOW;
404         retlen = ops.oobretlen;
405         if (copy_to_user(retp, &retlen, sizeof(length)))
406                 ret = -EFAULT;
407
408         kfree(ops.oobbuf);
409         return ret;
410 }
411
412 static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
413         uint64_t start, uint32_t length, void __user *ptr,
414         uint32_t __user *retp)
415 {
416         struct mtd_file_info *mfi = file->private_data;
417         struct mtd_oob_ops ops;
418         int ret = 0;
419
420         if (length > 4096)
421                 return -EINVAL;
422
423         if (!access_ok(VERIFY_WRITE, ptr, length))
424                 return -EFAULT;
425
426         ops.ooblen = length;
427         ops.ooboffs = start & (mtd->writesize - 1);
428         ops.datbuf = NULL;
429         ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
430                 MTD_OPS_PLACE_OOB;
431
432         if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
433                 return -EINVAL;
434
435         ops.oobbuf = kmalloc(length, GFP_KERNEL);
436         if (!ops.oobbuf)
437                 return -ENOMEM;
438
439         start &= ~((uint64_t)mtd->writesize - 1);
440         ret = mtd_read_oob(mtd, start, &ops);
441
442         if (put_user(ops.oobretlen, retp))
443                 ret = -EFAULT;
444         else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
445                                             ops.oobretlen))
446                 ret = -EFAULT;
447
448         kfree(ops.oobbuf);
449
450         /*
451          * NAND returns -EBADMSG on ECC errors, but it returns the OOB
452          * data. For our userspace tools it is important to dump areas
453          * with ECC errors!
454          * For kernel internal usage it also might return -EUCLEAN
455          * to signal the caller that a bitflip has occured and has
456          * been corrected by the ECC algorithm.
457          *
458          * Note: currently the standard NAND function, nand_read_oob_std,
459          * does not calculate ECC for the OOB area, so do not rely on
460          * this behavior unless you have replaced it with your own.
461          */
462         if (mtd_is_bitflip_or_eccerr(ret))
463                 return 0;
464
465         return ret;
466 }
467
468 /*
469  * Copies (and truncates, if necessary) data from the larger struct,
470  * nand_ecclayout, to the smaller, deprecated layout struct,
471  * nand_ecclayout_user. This is necessary only to support the deprecated
472  * API ioctl ECCGETLAYOUT while allowing all new functionality to use
473  * nand_ecclayout flexibly (i.e. the struct may change size in new
474  * releases without requiring major rewrites).
475  */
476 static int shrink_ecclayout(const struct nand_ecclayout *from,
477                 struct nand_ecclayout_user *to)
478 {
479         int i;
480
481         if (!from || !to)
482                 return -EINVAL;
483
484         memset(to, 0, sizeof(*to));
485
486         to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
487         for (i = 0; i < to->eccbytes; i++)
488                 to->eccpos[i] = from->eccpos[i];
489
490         for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
491                 if (from->oobfree[i].length == 0 &&
492                                 from->oobfree[i].offset == 0)
493                         break;
494                 to->oobavail += from->oobfree[i].length;
495                 to->oobfree[i] = from->oobfree[i];
496         }
497
498         return 0;
499 }
500
501 static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
502                                struct blkpg_ioctl_arg *arg)
503 {
504         struct blkpg_partition p;
505
506         if (!capable(CAP_SYS_ADMIN))
507                 return -EPERM;
508
509         if (copy_from_user(&p, arg->data, sizeof(p)))
510                 return -EFAULT;
511
512         switch (arg->op) {
513         case BLKPG_ADD_PARTITION:
514
515                 /* Only master mtd device must be used to add partitions */
516                 if (mtd_is_partition(mtd))
517                         return -EINVAL;
518
519                 /* Sanitize user input */
520                 p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
521
522                 return mtd_add_partition(mtd, p.devname, p.start, p.length);
523
524         case BLKPG_DEL_PARTITION:
525
526                 if (p.pno < 0)
527                         return -EINVAL;
528
529                 return mtd_del_partition(mtd, p.pno);
530
531         default:
532                 return -EINVAL;
533         }
534 }
535
536 static int mtdchar_write_ioctl(struct mtd_info *mtd,
537                 struct mtd_write_req __user *argp)
538 {
539         struct mtd_write_req req;
540         struct mtd_oob_ops ops;
541         const void __user *usr_data, *usr_oob;
542         int ret;
543
544         if (copy_from_user(&req, argp, sizeof(req)))
545                 return -EFAULT;
546
547         usr_data = (const void __user *)(uintptr_t)req.usr_data;
548         usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
549         if (!access_ok(VERIFY_READ, usr_data, req.len) ||
550             !access_ok(VERIFY_READ, usr_oob, req.ooblen))
551                 return -EFAULT;
552
553         if (!mtd->_write_oob)
554                 return -EOPNOTSUPP;
555
556         ops.mode = req.mode;
557         ops.len = (size_t)req.len;
558         ops.ooblen = (size_t)req.ooblen;
559         ops.ooboffs = 0;
560
561         if (usr_data) {
562                 ops.datbuf = memdup_user(usr_data, ops.len);
563                 if (IS_ERR(ops.datbuf))
564                         return PTR_ERR(ops.datbuf);
565         } else {
566                 ops.datbuf = NULL;
567         }
568
569         if (usr_oob) {
570                 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
571                 if (IS_ERR(ops.oobbuf)) {
572                         kfree(ops.datbuf);
573                         return PTR_ERR(ops.oobbuf);
574                 }
575         } else {
576                 ops.oobbuf = NULL;
577         }
578
579         ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
580
581         kfree(ops.datbuf);
582         kfree(ops.oobbuf);
583
584         return ret;
585 }
586
587 static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
588 {
589         struct mtd_file_info *mfi = file->private_data;
590         struct mtd_info *mtd = mfi->mtd;
591         void __user *argp = (void __user *)arg;
592         int ret = 0;
593         u_long size;
594         struct mtd_info_user info;
595
596         pr_debug("MTD_ioctl\n");
597
598         size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
599         if (cmd & IOC_IN) {
600                 if (!access_ok(VERIFY_READ, argp, size))
601                         return -EFAULT;
602         }
603         if (cmd & IOC_OUT) {
604                 if (!access_ok(VERIFY_WRITE, argp, size))
605                         return -EFAULT;
606         }
607
608         /*
609          * Check the file mode to require "dangerous" commands to have write
610          * permissions.
611          */
612         switch (cmd) {
613         /* "safe" commands */
614         case MEMGETREGIONCOUNT:
615         case MEMGETREGIONINFO:
616         case MEMGETINFO:
617         case MEMREADOOB:
618         case MEMREADOOB64:
619         case MEMISLOCKED:
620         case MEMGETOOBSEL:
621         case MEMGETBADBLOCK:
622         case OTPSELECT:
623         case OTPGETREGIONCOUNT:
624         case OTPGETREGIONINFO:
625         case ECCGETLAYOUT:
626         case ECCGETSTATS:
627         case MTDFILEMODE:
628         case BLKPG:
629         case BLKRRPART:
630                 break;
631
632         /* "dangerous" commands */
633         case MEMERASE:
634         case MEMERASE64:
635         case MEMLOCK:
636         case MEMUNLOCK:
637         case MEMSETBADBLOCK:
638         case MEMWRITEOOB:
639         case MEMWRITEOOB64:
640         case MEMWRITE:
641         case OTPLOCK:
642                 if (!(file->f_mode & FMODE_WRITE))
643                         return -EPERM;
644                 break;
645
646         default:
647                 return -ENOTTY;
648         }
649
650         switch (cmd) {
651         case MEMGETREGIONCOUNT:
652                 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
653                         return -EFAULT;
654                 break;
655
656         case MEMGETREGIONINFO:
657         {
658                 uint32_t ur_idx;
659                 struct mtd_erase_region_info *kr;
660                 struct region_info_user __user *ur = argp;
661
662                 if (get_user(ur_idx, &(ur->regionindex)))
663                         return -EFAULT;
664
665                 if (ur_idx >= mtd->numeraseregions)
666                         return -EINVAL;
667
668                 kr = &(mtd->eraseregions[ur_idx]);
669
670                 if (put_user(kr->offset, &(ur->offset))
671                     || put_user(kr->erasesize, &(ur->erasesize))
672                     || put_user(kr->numblocks, &(ur->numblocks)))
673                         return -EFAULT;
674
675                 break;
676         }
677
678         case MEMGETINFO:
679                 memset(&info, 0, sizeof(info));
680                 info.type       = mtd->type;
681                 info.flags      = mtd->flags;
682                 info.size       = mtd->size;
683                 info.erasesize  = mtd->erasesize;
684                 info.writesize  = mtd->writesize;
685                 info.oobsize    = mtd->oobsize;
686                 /* The below field is obsolete */
687                 info.padding    = 0;
688                 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
689                         return -EFAULT;
690                 break;
691
692         case MEMERASE:
693         case MEMERASE64:
694         {
695                 struct erase_info *erase;
696
697                 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
698                 if (!erase)
699                         ret = -ENOMEM;
700                 else {
701                         wait_queue_head_t waitq;
702                         DECLARE_WAITQUEUE(wait, current);
703
704                         init_waitqueue_head(&waitq);
705
706                         if (cmd == MEMERASE64) {
707                                 struct erase_info_user64 einfo64;
708
709                                 if (copy_from_user(&einfo64, argp,
710                                             sizeof(struct erase_info_user64))) {
711                                         kfree(erase);
712                                         return -EFAULT;
713                                 }
714                                 erase->addr = einfo64.start;
715                                 erase->len = einfo64.length;
716                         } else {
717                                 struct erase_info_user einfo32;
718
719                                 if (copy_from_user(&einfo32, argp,
720                                             sizeof(struct erase_info_user))) {
721                                         kfree(erase);
722                                         return -EFAULT;
723                                 }
724                                 erase->addr = einfo32.start;
725                                 erase->len = einfo32.length;
726                         }
727                         erase->mtd = mtd;
728                         erase->callback = mtdchar_erase_callback;
729                         erase->priv = (unsigned long)&waitq;
730
731                         /*
732                           FIXME: Allow INTERRUPTIBLE. Which means
733                           not having the wait_queue head on the stack.
734
735                           If the wq_head is on the stack, and we
736                           leave because we got interrupted, then the
737                           wq_head is no longer there when the
738                           callback routine tries to wake us up.
739                         */
740                         ret = mtd_erase(mtd, erase);
741                         if (!ret) {
742                                 set_current_state(TASK_UNINTERRUPTIBLE);
743                                 add_wait_queue(&waitq, &wait);
744                                 if (erase->state != MTD_ERASE_DONE &&
745                                     erase->state != MTD_ERASE_FAILED)
746                                         schedule();
747                                 remove_wait_queue(&waitq, &wait);
748                                 set_current_state(TASK_RUNNING);
749
750                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
751                         }
752                         kfree(erase);
753                 }
754                 break;
755         }
756
757         case MEMWRITEOOB:
758         {
759                 struct mtd_oob_buf buf;
760                 struct mtd_oob_buf __user *buf_user = argp;
761
762                 /* NOTE: writes return length to buf_user->length */
763                 if (copy_from_user(&buf, argp, sizeof(buf)))
764                         ret = -EFAULT;
765                 else
766                         ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
767                                 buf.ptr, &buf_user->length);
768                 break;
769         }
770
771         case MEMREADOOB:
772         {
773                 struct mtd_oob_buf buf;
774                 struct mtd_oob_buf __user *buf_user = argp;
775
776                 /* NOTE: writes return length to buf_user->start */
777                 if (copy_from_user(&buf, argp, sizeof(buf)))
778                         ret = -EFAULT;
779                 else
780                         ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
781                                 buf.ptr, &buf_user->start);
782                 break;
783         }
784
785         case MEMWRITEOOB64:
786         {
787                 struct mtd_oob_buf64 buf;
788                 struct mtd_oob_buf64 __user *buf_user = argp;
789
790                 if (copy_from_user(&buf, argp, sizeof(buf)))
791                         ret = -EFAULT;
792                 else
793                         ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
794                                 (void __user *)(uintptr_t)buf.usr_ptr,
795                                 &buf_user->length);
796                 break;
797         }
798
799         case MEMREADOOB64:
800         {
801                 struct mtd_oob_buf64 buf;
802                 struct mtd_oob_buf64 __user *buf_user = argp;
803
804                 if (copy_from_user(&buf, argp, sizeof(buf)))
805                         ret = -EFAULT;
806                 else
807                         ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
808                                 (void __user *)(uintptr_t)buf.usr_ptr,
809                                 &buf_user->length);
810                 break;
811         }
812
813         case MEMWRITE:
814         {
815                 ret = mtdchar_write_ioctl(mtd,
816                       (struct mtd_write_req __user *)arg);
817                 break;
818         }
819
820         case MEMLOCK:
821         {
822                 struct erase_info_user einfo;
823
824                 if (copy_from_user(&einfo, argp, sizeof(einfo)))
825                         return -EFAULT;
826
827                 ret = mtd_lock(mtd, einfo.start, einfo.length);
828                 break;
829         }
830
831         case MEMUNLOCK:
832         {
833                 struct erase_info_user einfo;
834
835                 if (copy_from_user(&einfo, argp, sizeof(einfo)))
836                         return -EFAULT;
837
838                 ret = mtd_unlock(mtd, einfo.start, einfo.length);
839                 break;
840         }
841
842         case MEMISLOCKED:
843         {
844                 struct erase_info_user einfo;
845
846                 if (copy_from_user(&einfo, argp, sizeof(einfo)))
847                         return -EFAULT;
848
849                 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
850                 break;
851         }
852
853         /* Legacy interface */
854         case MEMGETOOBSEL:
855         {
856                 struct nand_oobinfo oi;
857
858                 if (!mtd->ecclayout)
859                         return -EOPNOTSUPP;
860                 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
861                         return -EINVAL;
862
863                 oi.useecc = MTD_NANDECC_AUTOPLACE;
864                 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
865                 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
866                        sizeof(oi.oobfree));
867                 oi.eccbytes = mtd->ecclayout->eccbytes;
868
869                 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
870                         return -EFAULT;
871                 break;
872         }
873
874         case MEMGETBADBLOCK:
875         {
876                 loff_t offs;
877
878                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
879                         return -EFAULT;
880                 return mtd_block_isbad(mtd, offs);
881                 break;
882         }
883
884         case MEMSETBADBLOCK:
885         {
886                 loff_t offs;
887
888                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
889                         return -EFAULT;
890                 return mtd_block_markbad(mtd, offs);
891                 break;
892         }
893
894         case OTPSELECT:
895         {
896                 int mode;
897                 if (copy_from_user(&mode, argp, sizeof(int)))
898                         return -EFAULT;
899
900                 mfi->mode = MTD_FILE_MODE_NORMAL;
901
902                 ret = otp_select_filemode(mfi, mode);
903
904                 file->f_pos = 0;
905                 break;
906         }
907
908         case OTPGETREGIONCOUNT:
909         case OTPGETREGIONINFO:
910         {
911                 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
912                 size_t retlen;
913                 if (!buf)
914                         return -ENOMEM;
915                 switch (mfi->mode) {
916                 case MTD_FILE_MODE_OTP_FACTORY:
917                         ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
918                         break;
919                 case MTD_FILE_MODE_OTP_USER:
920                         ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
921                         break;
922                 default:
923                         ret = -EINVAL;
924                         break;
925                 }
926                 if (!ret) {
927                         if (cmd == OTPGETREGIONCOUNT) {
928                                 int nbr = retlen / sizeof(struct otp_info);
929                                 ret = copy_to_user(argp, &nbr, sizeof(int));
930                         } else
931                                 ret = copy_to_user(argp, buf, retlen);
932                         if (ret)
933                                 ret = -EFAULT;
934                 }
935                 kfree(buf);
936                 break;
937         }
938
939         case OTPLOCK:
940         {
941                 struct otp_info oinfo;
942
943                 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
944                         return -EINVAL;
945                 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
946                         return -EFAULT;
947                 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
948                 break;
949         }
950
951         /* This ioctl is being deprecated - it truncates the ECC layout */
952         case ECCGETLAYOUT:
953         {
954                 struct nand_ecclayout_user *usrlay;
955
956                 if (!mtd->ecclayout)
957                         return -EOPNOTSUPP;
958
959                 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
960                 if (!usrlay)
961                         return -ENOMEM;
962
963                 shrink_ecclayout(mtd->ecclayout, usrlay);
964
965                 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
966                         ret = -EFAULT;
967                 kfree(usrlay);
968                 break;
969         }
970
971         case ECCGETSTATS:
972         {
973                 if (copy_to_user(argp, &mtd->ecc_stats,
974                                  sizeof(struct mtd_ecc_stats)))
975                         return -EFAULT;
976                 break;
977         }
978
979         case MTDFILEMODE:
980         {
981                 mfi->mode = 0;
982
983                 switch(arg) {
984                 case MTD_FILE_MODE_OTP_FACTORY:
985                 case MTD_FILE_MODE_OTP_USER:
986                         ret = otp_select_filemode(mfi, arg);
987                         break;
988
989                 case MTD_FILE_MODE_RAW:
990                         if (!mtd_has_oob(mtd))
991                                 return -EOPNOTSUPP;
992                         mfi->mode = arg;
993
994                 case MTD_FILE_MODE_NORMAL:
995                         break;
996                 default:
997                         ret = -EINVAL;
998                 }
999                 file->f_pos = 0;
1000                 break;
1001         }
1002
1003         case BLKPG:
1004         {
1005                 struct blkpg_ioctl_arg __user *blk_arg = argp;
1006                 struct blkpg_ioctl_arg a;
1007
1008                 if (copy_from_user(&a, blk_arg, sizeof(a)))
1009                         ret = -EFAULT;
1010                 else
1011                         ret = mtdchar_blkpg_ioctl(mtd, &a);
1012                 break;
1013         }
1014
1015         case BLKRRPART:
1016         {
1017                 /* No reread partition feature. Just return ok */
1018                 ret = 0;
1019                 break;
1020         }
1021         }
1022
1023         return ret;
1024 } /* memory_ioctl */
1025
1026 static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
1027 {
1028         int ret;
1029
1030         mutex_lock(&mtd_mutex);
1031         ret = mtdchar_ioctl(file, cmd, arg);
1032         mutex_unlock(&mtd_mutex);
1033
1034         return ret;
1035 }
1036
1037 #ifdef CONFIG_COMPAT
1038
1039 struct mtd_oob_buf32 {
1040         u_int32_t start;
1041         u_int32_t length;
1042         compat_caddr_t ptr;     /* unsigned char* */
1043 };
1044
1045 #define MEMWRITEOOB32           _IOWR('M', 3, struct mtd_oob_buf32)
1046 #define MEMREADOOB32            _IOWR('M', 4, struct mtd_oob_buf32)
1047
1048 static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
1049         unsigned long arg)
1050 {
1051         struct mtd_file_info *mfi = file->private_data;
1052         struct mtd_info *mtd = mfi->mtd;
1053         void __user *argp = compat_ptr(arg);
1054         int ret = 0;
1055
1056         mutex_lock(&mtd_mutex);
1057
1058         switch (cmd) {
1059         case MEMWRITEOOB32:
1060         {
1061                 struct mtd_oob_buf32 buf;
1062                 struct mtd_oob_buf32 __user *buf_user = argp;
1063
1064                 if (!(file->f_mode & FMODE_WRITE)) {
1065                         ret = -EPERM;
1066                         break;
1067                 }
1068
1069                 if (copy_from_user(&buf, argp, sizeof(buf)))
1070                         ret = -EFAULT;
1071                 else
1072                         ret = mtdchar_writeoob(file, mtd, buf.start,
1073                                 buf.length, compat_ptr(buf.ptr),
1074                                 &buf_user->length);
1075                 break;
1076         }
1077
1078         case MEMREADOOB32:
1079         {
1080                 struct mtd_oob_buf32 buf;
1081                 struct mtd_oob_buf32 __user *buf_user = argp;
1082
1083                 /* NOTE: writes return length to buf->start */
1084                 if (copy_from_user(&buf, argp, sizeof(buf)))
1085                         ret = -EFAULT;
1086                 else
1087                         ret = mtdchar_readoob(file, mtd, buf.start,
1088                                 buf.length, compat_ptr(buf.ptr),
1089                                 &buf_user->start);
1090                 break;
1091         }
1092
1093         case BLKPG:
1094         {
1095                 /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
1096                 struct blkpg_compat_ioctl_arg __user *uarg = argp;
1097                 struct blkpg_compat_ioctl_arg compat_arg;
1098                 struct blkpg_ioctl_arg a;
1099
1100                 if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
1101                         ret = -EFAULT;
1102                         break;
1103                 }
1104
1105                 memset(&a, 0, sizeof(a));
1106                 a.op = compat_arg.op;
1107                 a.flags = compat_arg.flags;
1108                 a.datalen = compat_arg.datalen;
1109                 a.data = compat_ptr(compat_arg.data);
1110
1111                 ret = mtdchar_blkpg_ioctl(mtd, &a);
1112                 break;
1113         }
1114
1115         default:
1116                 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
1117         }
1118
1119         mutex_unlock(&mtd_mutex);
1120
1121         return ret;
1122 }
1123
1124 #endif /* CONFIG_COMPAT */
1125
1126 /*
1127  * try to determine where a shared mapping can be made
1128  * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1129  *   mappings)
1130  */
1131 #ifndef CONFIG_MMU
1132 static unsigned long mtdchar_get_unmapped_area(struct file *file,
1133                                            unsigned long addr,
1134                                            unsigned long len,
1135                                            unsigned long pgoff,
1136                                            unsigned long flags)
1137 {
1138         struct mtd_file_info *mfi = file->private_data;
1139         struct mtd_info *mtd = mfi->mtd;
1140         unsigned long offset;
1141         int ret;
1142
1143         if (addr != 0)
1144                 return (unsigned long) -EINVAL;
1145
1146         if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1147                 return (unsigned long) -EINVAL;
1148
1149         offset = pgoff << PAGE_SHIFT;
1150         if (offset > mtd->size - len)
1151                 return (unsigned long) -EINVAL;
1152
1153         ret = mtd_get_unmapped_area(mtd, len, offset, flags);
1154         return ret == -EOPNOTSUPP ? -ENODEV : ret;
1155 }
1156
1157 static unsigned mtdchar_mmap_capabilities(struct file *file)
1158 {
1159         struct mtd_file_info *mfi = file->private_data;
1160
1161         return mtd_mmap_capabilities(mfi->mtd);
1162 }
1163 #endif
1164
1165 /*
1166  * set up a mapping for shared memory segments
1167  */
1168 static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
1169 {
1170 #ifdef CONFIG_MMU
1171         struct mtd_file_info *mfi = file->private_data;
1172         struct mtd_info *mtd = mfi->mtd;
1173         struct map_info *map = mtd->priv;
1174
1175         /* This is broken because it assumes the MTD device is map-based
1176            and that mtd->priv is a valid struct map_info.  It should be
1177            replaced with something that uses the mtd_get_unmapped_area()
1178            operation properly. */
1179         if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
1180 #ifdef pgprot_noncached
1181                 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
1182                         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1183 #endif
1184                 return vm_iomap_memory(vma, map->phys, map->size);
1185         }
1186         return -ENODEV;
1187 #else
1188         return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
1189 #endif
1190 }
1191
1192 static const struct file_operations mtd_fops = {
1193         .owner          = THIS_MODULE,
1194         .llseek         = mtdchar_lseek,
1195         .read           = mtdchar_read,
1196         .write          = mtdchar_write,
1197         .unlocked_ioctl = mtdchar_unlocked_ioctl,
1198 #ifdef CONFIG_COMPAT
1199         .compat_ioctl   = mtdchar_compat_ioctl,
1200 #endif
1201         .open           = mtdchar_open,
1202         .release        = mtdchar_close,
1203         .mmap           = mtdchar_mmap,
1204 #ifndef CONFIG_MMU
1205         .get_unmapped_area = mtdchar_get_unmapped_area,
1206         .mmap_capabilities = mtdchar_mmap_capabilities,
1207 #endif
1208 };
1209
1210 int __init init_mtdchar(void)
1211 {
1212         int ret;
1213
1214         ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
1215                                    "mtd", &mtd_fops);
1216         if (ret < 0) {
1217                 pr_err("Can't allocate major number %d for MTD\n",
1218                        MTD_CHAR_MAJOR);
1219                 return ret;
1220         }
1221
1222         return ret;
1223 }
1224
1225 void __exit cleanup_mtdchar(void)
1226 {
1227         __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1228 }
1229
1230 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);