blob: 18dd333f2d4075054f50a90f123b399533915e62 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <linux/uaccess.h>
41
42#include "mtdcore.h"
43
44static 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 */
50struct mtd_file_info {
51 struct mtd_info *mtd;
52 enum mtd_file_modes mode;
53};
54
55static 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
61static 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
104out1:
105 put_mtd_device(mtd);
106out:
107 mutex_unlock(&mtd_mutex);
108 return ret;
109} /* mtdchar_open */
110
111/*====================================================================*/
112
113static 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
149static 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
239static 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======================================================================*/
331static void mtdchar_erase_callback (struct erase_info *instr)
332{
333 wake_up((wait_queue_head_t *)instr->priv);
334}
335
336static 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
366static 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
412static 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 occurred 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) OOB layout information to the
470 * deprecated layout struct, nand_ecclayout_user. This is necessary only to
471 * support the deprecated API ioctl ECCGETLAYOUT while allowing all new
472 * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
473 * can describe any kind of OOB layout with almost zero overhead from a
474 * memory usage point of view).
475 */
476static int shrink_ecclayout(struct mtd_info *mtd,
477 struct nand_ecclayout_user *to)
478{
479 struct mtd_oob_region oobregion;
480 int i, section = 0, ret;
481
482 if (!mtd || !to)
483 return -EINVAL;
484
485 memset(to, 0, sizeof(*to));
486
487 to->eccbytes = 0;
488 for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
489 u32 eccpos;
490
491 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
492 if (ret < 0) {
493 if (ret != -ERANGE)
494 return ret;
495
496 break;
497 }
498
499 eccpos = oobregion.offset;
500 for (; i < MTD_MAX_ECCPOS_ENTRIES &&
501 eccpos < oobregion.offset + oobregion.length; i++) {
502 to->eccpos[i] = eccpos++;
503 to->eccbytes++;
504 }
505 }
506
507 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
508 ret = mtd_ooblayout_free(mtd, i, &oobregion);
509 if (ret < 0) {
510 if (ret != -ERANGE)
511 return ret;
512
513 break;
514 }
515
516 to->oobfree[i].offset = oobregion.offset;
517 to->oobfree[i].length = oobregion.length;
518 to->oobavail += to->oobfree[i].length;
519 }
520
521 return 0;
522}
523
524static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
525{
526 struct mtd_oob_region oobregion;
527 int i, section = 0, ret;
528
529 if (!mtd || !to)
530 return -EINVAL;
531
532 memset(to, 0, sizeof(*to));
533
534 to->eccbytes = 0;
535 for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
536 u32 eccpos;
537
538 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
539 if (ret < 0) {
540 if (ret != -ERANGE)
541 return ret;
542
543 break;
544 }
545
546 if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
547 return -EINVAL;
548
549 eccpos = oobregion.offset;
550 for (; eccpos < oobregion.offset + oobregion.length; i++) {
551 to->eccpos[i] = eccpos++;
552 to->eccbytes++;
553 }
554 }
555
556 for (i = 0; i < 8; i++) {
557 ret = mtd_ooblayout_free(mtd, i, &oobregion);
558 if (ret < 0) {
559 if (ret != -ERANGE)
560 return ret;
561
562 break;
563 }
564
565 to->oobfree[i][0] = oobregion.offset;
566 to->oobfree[i][1] = oobregion.length;
567 }
568
569 to->useecc = MTD_NANDECC_AUTOPLACE;
570
571 return 0;
572}
573
574static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
575 struct blkpg_ioctl_arg *arg)
576{
577 struct blkpg_partition p;
578
579 if (!capable(CAP_SYS_ADMIN))
580 return -EPERM;
581
582 if (copy_from_user(&p, arg->data, sizeof(p)))
583 return -EFAULT;
584
585 switch (arg->op) {
586 case BLKPG_ADD_PARTITION:
587
588 /* Only master mtd device must be used to add partitions */
589 if (mtd_is_partition(mtd))
590 return -EINVAL;
591
592 /* Sanitize user input */
593 p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
594
595 return mtd_add_partition(mtd, p.devname, p.start, p.length);
596
597 case BLKPG_DEL_PARTITION:
598
599 if (p.pno < 0)
600 return -EINVAL;
601
602 return mtd_del_partition(mtd, p.pno);
603
604 default:
605 return -EINVAL;
606 }
607}
608
609static int mtdchar_write_ioctl(struct mtd_info *mtd,
610 struct mtd_write_req __user *argp)
611{
612 struct mtd_write_req req;
613 struct mtd_oob_ops ops;
614 const void __user *usr_data, *usr_oob;
615 int ret;
616
617 if (copy_from_user(&req, argp, sizeof(req)))
618 return -EFAULT;
619
620 usr_data = (const void __user *)(uintptr_t)req.usr_data;
621 usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
622 if (!access_ok(VERIFY_READ, usr_data, req.len) ||
623 !access_ok(VERIFY_READ, usr_oob, req.ooblen))
624 return -EFAULT;
625
626 if (!mtd->_write_oob)
627 return -EOPNOTSUPP;
628
629 ops.mode = req.mode;
630 ops.len = (size_t)req.len;
631 ops.ooblen = (size_t)req.ooblen;
632 ops.ooboffs = 0;
633
634 if (usr_data) {
635 ops.datbuf = memdup_user(usr_data, ops.len);
636 if (IS_ERR(ops.datbuf))
637 return PTR_ERR(ops.datbuf);
638 } else {
639 ops.datbuf = NULL;
640 }
641
642 if (usr_oob) {
643 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
644 if (IS_ERR(ops.oobbuf)) {
645 kfree(ops.datbuf);
646 return PTR_ERR(ops.oobbuf);
647 }
648 } else {
649 ops.oobbuf = NULL;
650 }
651
652 ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
653
654 kfree(ops.datbuf);
655 kfree(ops.oobbuf);
656
657 return ret;
658}
659
660static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
661{
662 struct mtd_file_info *mfi = file->private_data;
663 struct mtd_info *mtd = mfi->mtd;
664 void __user *argp = (void __user *)arg;
665 int ret = 0;
666 u_long size;
667 struct mtd_info_user info;
668
669 pr_debug("MTD_ioctl\n");
670
671 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
672 if (cmd & IOC_IN) {
673 if (!access_ok(VERIFY_READ, argp, size))
674 return -EFAULT;
675 }
676 if (cmd & IOC_OUT) {
677 if (!access_ok(VERIFY_WRITE, argp, size))
678 return -EFAULT;
679 }
680
681 /*
682 * Check the file mode to require "dangerous" commands to have write
683 * permissions.
684 */
685 switch (cmd) {
686 /* "safe" commands */
687 case MEMGETREGIONCOUNT:
688 case MEMGETREGIONINFO:
689 case MEMGETINFO:
690 case MEMREADOOB:
691 case MEMREADOOB64:
692 case MEMLOCK:
693 case MEMUNLOCK:
694 case MEMISLOCKED:
695 case MEMGETOOBSEL:
696 case MEMGETBADBLOCK:
697 case MEMSETBADBLOCK:
698 case OTPSELECT:
699 case OTPGETREGIONCOUNT:
700 case OTPGETREGIONINFO:
701 case OTPLOCK:
702 case ECCGETLAYOUT:
703 case ECCGETSTATS:
704 case MTDFILEMODE:
705 case BLKPG:
706 case BLKRRPART:
707 break;
708
709 /* "dangerous" commands */
710 case MEMERASE:
711 case MEMERASE64:
712 case MEMWRITEOOB:
713 case MEMWRITEOOB64:
714 case MEMWRITE:
715 if (!(file->f_mode & FMODE_WRITE))
716 return -EPERM;
717 break;
718
719 default:
720 return -ENOTTY;
721 }
722
723 switch (cmd) {
724 case MEMGETREGIONCOUNT:
725 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
726 return -EFAULT;
727 break;
728
729 case MEMGETREGIONINFO:
730 {
731 uint32_t ur_idx;
732 struct mtd_erase_region_info *kr;
733 struct region_info_user __user *ur = argp;
734
735 if (get_user(ur_idx, &(ur->regionindex)))
736 return -EFAULT;
737
738 if (ur_idx >= mtd->numeraseregions)
739 return -EINVAL;
740
741 kr = &(mtd->eraseregions[ur_idx]);
742
743 if (put_user(kr->offset, &(ur->offset))
744 || put_user(kr->erasesize, &(ur->erasesize))
745 || put_user(kr->numblocks, &(ur->numblocks)))
746 return -EFAULT;
747
748 break;
749 }
750
751 case MEMGETINFO:
752 memset(&info, 0, sizeof(info));
753 info.type = mtd->type;
754 info.flags = mtd->flags;
755 info.size = mtd->size;
756 info.erasesize = mtd->erasesize;
757 info.writesize = mtd->writesize;
758 info.oobsize = mtd->oobsize;
759 /* The below field is obsolete */
760 info.padding = 0;
761 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
762 return -EFAULT;
763 break;
764
765 case MEMERASE:
766 case MEMERASE64:
767 {
768 struct erase_info *erase;
769
770 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
771 if (!erase)
772 ret = -ENOMEM;
773 else {
774 wait_queue_head_t waitq;
775 DECLARE_WAITQUEUE(wait, current);
776
777 init_waitqueue_head(&waitq);
778
779 if (cmd == MEMERASE64) {
780 struct erase_info_user64 einfo64;
781
782 if (copy_from_user(&einfo64, argp,
783 sizeof(struct erase_info_user64))) {
784 kfree(erase);
785 return -EFAULT;
786 }
787 erase->addr = einfo64.start;
788 erase->len = einfo64.length;
789 } else {
790 struct erase_info_user einfo32;
791
792 if (copy_from_user(&einfo32, argp,
793 sizeof(struct erase_info_user))) {
794 kfree(erase);
795 return -EFAULT;
796 }
797 erase->addr = einfo32.start;
798 erase->len = einfo32.length;
799 }
800 erase->mtd = mtd;
801 erase->callback = mtdchar_erase_callback;
802 erase->priv = (unsigned long)&waitq;
803
804 /*
805 FIXME: Allow INTERRUPTIBLE. Which means
806 not having the wait_queue head on the stack.
807
808 If the wq_head is on the stack, and we
809 leave because we got interrupted, then the
810 wq_head is no longer there when the
811 callback routine tries to wake us up.
812 */
813 ret = mtd_erase(mtd, erase);
814 if (!ret) {
815 set_current_state(TASK_UNINTERRUPTIBLE);
816 add_wait_queue(&waitq, &wait);
817 if (erase->state != MTD_ERASE_DONE &&
818 erase->state != MTD_ERASE_FAILED)
819 schedule();
820 remove_wait_queue(&waitq, &wait);
821 set_current_state(TASK_RUNNING);
822
823 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
824 }
825 kfree(erase);
826 }
827 break;
828 }
829
830 case MEMWRITEOOB:
831 {
832 struct mtd_oob_buf buf;
833 struct mtd_oob_buf __user *buf_user = argp;
834
835 /* NOTE: writes return length to buf_user->length */
836 if (copy_from_user(&buf, argp, sizeof(buf)))
837 ret = -EFAULT;
838 else
839 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
840 buf.ptr, &buf_user->length);
841 break;
842 }
843
844 case MEMREADOOB:
845 {
846 struct mtd_oob_buf buf;
847 struct mtd_oob_buf __user *buf_user = argp;
848
849 /* NOTE: writes return length to buf_user->start */
850 if (copy_from_user(&buf, argp, sizeof(buf)))
851 ret = -EFAULT;
852 else
853 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
854 buf.ptr, &buf_user->start);
855 break;
856 }
857
858 case MEMWRITEOOB64:
859 {
860 struct mtd_oob_buf64 buf;
861 struct mtd_oob_buf64 __user *buf_user = argp;
862
863 if (copy_from_user(&buf, argp, sizeof(buf)))
864 ret = -EFAULT;
865 else
866 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
867 (void __user *)(uintptr_t)buf.usr_ptr,
868 &buf_user->length);
869 break;
870 }
871
872 case MEMREADOOB64:
873 {
874 struct mtd_oob_buf64 buf;
875 struct mtd_oob_buf64 __user *buf_user = argp;
876
877 if (copy_from_user(&buf, argp, sizeof(buf)))
878 ret = -EFAULT;
879 else
880 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
881 (void __user *)(uintptr_t)buf.usr_ptr,
882 &buf_user->length);
883 break;
884 }
885
886 case MEMWRITE:
887 {
888 ret = mtdchar_write_ioctl(mtd,
889 (struct mtd_write_req __user *)arg);
890 break;
891 }
892
893 case MEMLOCK:
894 {
895 struct erase_info_user einfo;
896
897 if (copy_from_user(&einfo, argp, sizeof(einfo)))
898 return -EFAULT;
899
900 ret = mtd_lock(mtd, einfo.start, einfo.length);
901 break;
902 }
903
904 case MEMUNLOCK:
905 {
906 struct erase_info_user einfo;
907
908 if (copy_from_user(&einfo, argp, sizeof(einfo)))
909 return -EFAULT;
910
911 ret = mtd_unlock(mtd, einfo.start, einfo.length);
912 break;
913 }
914
915 case MEMISLOCKED:
916 {
917 struct erase_info_user einfo;
918
919 if (copy_from_user(&einfo, argp, sizeof(einfo)))
920 return -EFAULT;
921
922 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
923 break;
924 }
925
926 /* Legacy interface */
927 case MEMGETOOBSEL:
928 {
929 struct nand_oobinfo oi;
930
931 if (!mtd->ooblayout)
932 return -EOPNOTSUPP;
933
934 ret = get_oobinfo(mtd, &oi);
935 if (ret)
936 return ret;
937
938 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
939 return -EFAULT;
940 break;
941 }
942
943 case MEMGETBADBLOCK:
944 {
945 loff_t offs;
946
947 if (copy_from_user(&offs, argp, sizeof(loff_t)))
948 return -EFAULT;
949 return mtd_block_isbad(mtd, offs);
950 break;
951 }
952
953 case MEMSETBADBLOCK:
954 {
955 loff_t offs;
956
957 if (copy_from_user(&offs, argp, sizeof(loff_t)))
958 return -EFAULT;
959 return mtd_block_markbad(mtd, offs);
960 break;
961 }
962
963 case OTPSELECT:
964 {
965 int mode;
966 if (copy_from_user(&mode, argp, sizeof(int)))
967 return -EFAULT;
968
969 mfi->mode = MTD_FILE_MODE_NORMAL;
970
971 ret = otp_select_filemode(mfi, mode);
972
973 file->f_pos = 0;
974 break;
975 }
976
977 case OTPGETREGIONCOUNT:
978 case OTPGETREGIONINFO:
979 {
980 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
981 size_t retlen;
982 if (!buf)
983 return -ENOMEM;
984 switch (mfi->mode) {
985 case MTD_FILE_MODE_OTP_FACTORY:
986 ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
987 break;
988 case MTD_FILE_MODE_OTP_USER:
989 ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
990 break;
991 default:
992 ret = -EINVAL;
993 break;
994 }
995 if (!ret) {
996 if (cmd == OTPGETREGIONCOUNT) {
997 int nbr = retlen / sizeof(struct otp_info);
998 ret = copy_to_user(argp, &nbr, sizeof(int));
999 } else
1000 ret = copy_to_user(argp, buf, retlen);
1001 if (ret)
1002 ret = -EFAULT;
1003 }
1004 kfree(buf);
1005 break;
1006 }
1007
1008 case OTPLOCK:
1009 {
1010 struct otp_info oinfo;
1011
1012 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
1013 return -EINVAL;
1014 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
1015 return -EFAULT;
1016 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
1017 break;
1018 }
1019
1020 /* This ioctl is being deprecated - it truncates the ECC layout */
1021 case ECCGETLAYOUT:
1022 {
1023 struct nand_ecclayout_user *usrlay;
1024
1025 if (!mtd->ooblayout)
1026 return -EOPNOTSUPP;
1027
1028 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
1029 if (!usrlay)
1030 return -ENOMEM;
1031
1032 shrink_ecclayout(mtd, usrlay);
1033
1034 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
1035 ret = -EFAULT;
1036 kfree(usrlay);
1037 break;
1038 }
1039
1040 case ECCGETSTATS:
1041 {
1042 if (copy_to_user(argp, &mtd->ecc_stats,
1043 sizeof(struct mtd_ecc_stats)))
1044 return -EFAULT;
1045 break;
1046 }
1047
1048 case MTDFILEMODE:
1049 {
1050 mfi->mode = 0;
1051
1052 switch(arg) {
1053 case MTD_FILE_MODE_OTP_FACTORY:
1054 case MTD_FILE_MODE_OTP_USER:
1055 ret = otp_select_filemode(mfi, arg);
1056 break;
1057
1058 case MTD_FILE_MODE_RAW:
1059 if (!mtd_has_oob(mtd))
1060 return -EOPNOTSUPP;
1061 mfi->mode = arg;
1062
1063 case MTD_FILE_MODE_NORMAL:
1064 break;
1065 default:
1066 ret = -EINVAL;
1067 }
1068 file->f_pos = 0;
1069 break;
1070 }
1071
1072 case BLKPG:
1073 {
1074 struct blkpg_ioctl_arg __user *blk_arg = argp;
1075 struct blkpg_ioctl_arg a;
1076
1077 if (copy_from_user(&a, blk_arg, sizeof(a)))
1078 ret = -EFAULT;
1079 else
1080 ret = mtdchar_blkpg_ioctl(mtd, &a);
1081 break;
1082 }
1083
1084 case BLKRRPART:
1085 {
1086 /* No reread partition feature. Just return ok */
1087 ret = 0;
1088 break;
1089 }
1090 }
1091
1092 return ret;
1093} /* memory_ioctl */
1094
1095static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
1096{
1097 int ret;
1098
1099 mutex_lock(&mtd_mutex);
1100 ret = mtdchar_ioctl(file, cmd, arg);
1101 mutex_unlock(&mtd_mutex);
1102
1103 return ret;
1104}
1105
1106#ifdef CONFIG_COMPAT
1107
1108struct mtd_oob_buf32 {
1109 u_int32_t start;
1110 u_int32_t length;
1111 compat_caddr_t ptr; /* unsigned char* */
1112};
1113
1114#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1115#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1116
1117static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
1118 unsigned long arg)
1119{
1120 struct mtd_file_info *mfi = file->private_data;
1121 struct mtd_info *mtd = mfi->mtd;
1122 void __user *argp = compat_ptr(arg);
1123 int ret = 0;
1124
1125 mutex_lock(&mtd_mutex);
1126
1127 switch (cmd) {
1128 case MEMWRITEOOB32:
1129 {
1130 struct mtd_oob_buf32 buf;
1131 struct mtd_oob_buf32 __user *buf_user = argp;
1132
1133 if (!(file->f_mode & FMODE_WRITE)) {
1134 ret = -EPERM;
1135 break;
1136 }
1137
1138 if (copy_from_user(&buf, argp, sizeof(buf)))
1139 ret = -EFAULT;
1140 else
1141 ret = mtdchar_writeoob(file, mtd, buf.start,
1142 buf.length, compat_ptr(buf.ptr),
1143 &buf_user->length);
1144 break;
1145 }
1146
1147 case MEMREADOOB32:
1148 {
1149 struct mtd_oob_buf32 buf;
1150 struct mtd_oob_buf32 __user *buf_user = argp;
1151
1152 /* NOTE: writes return length to buf->start */
1153 if (copy_from_user(&buf, argp, sizeof(buf)))
1154 ret = -EFAULT;
1155 else
1156 ret = mtdchar_readoob(file, mtd, buf.start,
1157 buf.length, compat_ptr(buf.ptr),
1158 &buf_user->start);
1159 break;
1160 }
1161
1162 case BLKPG:
1163 {
1164 /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
1165 struct blkpg_compat_ioctl_arg __user *uarg = argp;
1166 struct blkpg_compat_ioctl_arg compat_arg;
1167 struct blkpg_ioctl_arg a;
1168
1169 if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
1170 ret = -EFAULT;
1171 break;
1172 }
1173
1174 memset(&a, 0, sizeof(a));
1175 a.op = compat_arg.op;
1176 a.flags = compat_arg.flags;
1177 a.datalen = compat_arg.datalen;
1178 a.data = compat_ptr(compat_arg.data);
1179
1180 ret = mtdchar_blkpg_ioctl(mtd, &a);
1181 break;
1182 }
1183
1184 default:
1185 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
1186 }
1187
1188 mutex_unlock(&mtd_mutex);
1189
1190 return ret;
1191}
1192
1193#endif /* CONFIG_COMPAT */
1194
1195/*
1196 * try to determine where a shared mapping can be made
1197 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1198 * mappings)
1199 */
1200#ifndef CONFIG_MMU
1201static unsigned long mtdchar_get_unmapped_area(struct file *file,
1202 unsigned long addr,
1203 unsigned long len,
1204 unsigned long pgoff,
1205 unsigned long flags)
1206{
1207 struct mtd_file_info *mfi = file->private_data;
1208 struct mtd_info *mtd = mfi->mtd;
1209 unsigned long offset;
1210 int ret;
1211
1212 if (addr != 0)
1213 return (unsigned long) -EINVAL;
1214
1215 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1216 return (unsigned long) -EINVAL;
1217
1218 offset = pgoff << PAGE_SHIFT;
1219 if (offset > mtd->size - len)
1220 return (unsigned long) -EINVAL;
1221
1222 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
1223 return ret == -EOPNOTSUPP ? -ENODEV : ret;
1224}
1225
1226static unsigned mtdchar_mmap_capabilities(struct file *file)
1227{
1228 struct mtd_file_info *mfi = file->private_data;
1229
1230 return mtd_mmap_capabilities(mfi->mtd);
1231}
1232#endif
1233
1234/*
1235 * set up a mapping for shared memory segments
1236 */
1237static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
1238{
1239#ifdef CONFIG_MMU
1240 struct mtd_file_info *mfi = file->private_data;
1241 struct mtd_info *mtd = mfi->mtd;
1242 struct map_info *map = mtd->priv;
1243
1244 /* This is broken because it assumes the MTD device is map-based
1245 and that mtd->priv is a valid struct map_info. It should be
1246 replaced with something that uses the mtd_get_unmapped_area()
1247 operation properly. */
1248 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
1249#ifdef pgprot_noncached
1250 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
1251 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1252#endif
1253 return vm_iomap_memory(vma, map->phys, map->size);
1254 }
1255 return -ENODEV;
1256#else
1257 return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
1258#endif
1259}
1260
1261static const struct file_operations mtd_fops = {
1262 .owner = THIS_MODULE,
1263 .llseek = mtdchar_lseek,
1264 .read = mtdchar_read,
1265 .write = mtdchar_write,
1266 .unlocked_ioctl = mtdchar_unlocked_ioctl,
1267#ifdef CONFIG_COMPAT
1268 .compat_ioctl = mtdchar_compat_ioctl,
1269#endif
1270 .open = mtdchar_open,
1271 .release = mtdchar_close,
1272 .mmap = mtdchar_mmap,
1273#ifndef CONFIG_MMU
1274 .get_unmapped_area = mtdchar_get_unmapped_area,
1275 .mmap_capabilities = mtdchar_mmap_capabilities,
1276#endif
1277};
1278
1279int __init init_mtdchar(void)
1280{
1281 int ret;
1282
1283 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
1284 "mtd", &mtd_fops);
1285 if (ret < 0) {
1286 pr_err("Can't allocate major number %d for MTD\n",
1287 MTD_CHAR_MAJOR);
1288 return ret;
1289 }
1290
1291 return ret;
1292}
1293
1294void __exit cleanup_mtdchar(void)
1295{
1296 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1297}
1298
1299MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);