2 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
20 #include <linux/device.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/mtd/mtd.h>
36 #include <linux/mtd/partitions.h>
37 #include <linux/mtd/map.h>
39 #include <asm/uaccess.h>
43 static DEFINE_MUTEX(mtd_mutex
);
46 * Data structure to hold the pointer to the mtd device as well
47 * as mode information of various use cases.
49 struct mtd_file_info
{
52 enum mtd_file_modes mode
;
55 static loff_t
mtdchar_lseek(struct file
*file
, loff_t offset
, int orig
)
57 struct mtd_file_info
*mfi
= file
->private_data
;
58 return fixed_size_llseek(file
, offset
, orig
, mfi
->mtd
->size
);
62 static struct vfsmount
*mnt
;
63 static struct file_system_type mtd_inodefs_type
;
65 static int mtdchar_open(struct inode
*inode
, struct file
*file
)
67 int minor
= iminor(inode
);
68 int devnum
= minor
>> 1;
71 struct mtd_file_info
*mfi
;
72 struct inode
*mtd_ino
;
74 pr_debug("MTD_open\n");
76 /* You can't open the RO devices RW */
77 if ((file
->f_mode
& FMODE_WRITE
) && (minor
& 1))
80 ret
= simple_pin_fs(&mtd_inodefs_type
, &mnt
, &count
);
84 mutex_lock(&mtd_mutex
);
85 mtd
= get_mtd_device(NULL
, devnum
);
92 if (mtd
->type
== MTD_ABSENT
) {
97 mtd_ino
= iget_locked(mnt
->mnt_sb
, devnum
);
102 if (mtd_ino
->i_state
& I_NEW
) {
103 mtd_ino
->i_private
= mtd
;
104 mtd_ino
->i_mode
= S_IFCHR
;
105 mtd_ino
->i_data
.backing_dev_info
= mtd
->backing_dev_info
;
106 unlock_new_inode(mtd_ino
);
108 file
->f_mapping
= mtd_ino
->i_mapping
;
110 /* You can't open it RW if it's not a writeable device */
111 if ((file
->f_mode
& FMODE_WRITE
) && !(mtd
->flags
& MTD_WRITEABLE
)) {
116 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
123 file
->private_data
= mfi
;
124 mutex_unlock(&mtd_mutex
);
132 mutex_unlock(&mtd_mutex
);
133 simple_release_fs(&mnt
, &count
);
137 /*====================================================================*/
139 static int mtdchar_close(struct inode
*inode
, struct file
*file
)
141 struct mtd_file_info
*mfi
= file
->private_data
;
142 struct mtd_info
*mtd
= mfi
->mtd
;
144 pr_debug("MTD_close\n");
146 /* Only sync if opened RW */
147 if ((file
->f_mode
& FMODE_WRITE
))
153 file
->private_data
= NULL
;
155 simple_release_fs(&mnt
, &count
);
158 } /* mtdchar_close */
160 /* Back in June 2001, dwmw2 wrote:
162 * FIXME: This _really_ needs to die. In 2.5, we should lock the
163 * userspace buffer down and use it directly with readv/writev.
165 * The implementation below, using mtd_kmalloc_up_to, mitigates
166 * allocation failures when the system is under low-memory situations
167 * or if memory is highly fragmented at the cost of reducing the
168 * performance of the requested transfer due to a smaller buffer size.
170 * A more complex but more memory-efficient implementation based on
171 * get_user_pages and iovecs to cover extents of those pages is a
172 * longer-term goal, as intimated by dwmw2 above. However, for the
173 * write case, this requires yet more complex head and tail transfer
174 * handling when those head and tail offsets and sizes are such that
175 * alignment requirements are not met in the NAND subdriver.
178 static ssize_t
mtdchar_read(struct file
*file
, char __user
*buf
, size_t count
,
181 struct mtd_file_info
*mfi
= file
->private_data
;
182 struct mtd_info
*mtd
= mfi
->mtd
;
184 size_t total_retlen
=0;
190 pr_debug("MTD_read\n");
192 if (*ppos
+ count
> mtd
->size
)
193 count
= mtd
->size
- *ppos
;
198 kbuf
= mtd_kmalloc_up_to(mtd
, &size
);
203 len
= min_t(size_t, count
, size
);
206 case MTD_FILE_MODE_OTP_FACTORY
:
207 ret
= mtd_read_fact_prot_reg(mtd
, *ppos
, len
,
210 case MTD_FILE_MODE_OTP_USER
:
211 ret
= mtd_read_user_prot_reg(mtd
, *ppos
, len
,
214 case MTD_FILE_MODE_RAW
:
216 struct mtd_oob_ops ops
;
218 ops
.mode
= MTD_OPS_RAW
;
223 ret
= mtd_read_oob(mtd
, *ppos
, &ops
);
228 ret
= mtd_read(mtd
, *ppos
, len
, &retlen
, kbuf
);
230 /* Nand returns -EBADMSG on ECC errors, but it returns
231 * the data. For our userspace tools it is important
232 * to dump areas with ECC errors!
233 * For kernel internal usage it also might return -EUCLEAN
234 * to signal the caller that a bitflip has occurred and has
235 * been corrected by the ECC algorithm.
236 * Userspace software which accesses NAND this way
237 * must be aware of the fact that it deals with NAND
239 if (!ret
|| mtd_is_bitflip_or_eccerr(ret
)) {
241 if (copy_to_user(buf
, kbuf
, retlen
)) {
246 total_retlen
+= retlen
;
264 static ssize_t
mtdchar_write(struct file
*file
, const char __user
*buf
, size_t count
,
267 struct mtd_file_info
*mfi
= file
->private_data
;
268 struct mtd_info
*mtd
= mfi
->mtd
;
272 size_t total_retlen
=0;
276 pr_debug("MTD_write\n");
278 if (*ppos
== mtd
->size
)
281 if (*ppos
+ count
> mtd
->size
)
282 count
= mtd
->size
- *ppos
;
287 kbuf
= mtd_kmalloc_up_to(mtd
, &size
);
292 len
= min_t(size_t, count
, size
);
294 if (copy_from_user(kbuf
, buf
, len
)) {
300 case MTD_FILE_MODE_OTP_FACTORY
:
303 case MTD_FILE_MODE_OTP_USER
:
304 ret
= mtd_write_user_prot_reg(mtd
, *ppos
, len
,
308 case MTD_FILE_MODE_RAW
:
310 struct mtd_oob_ops ops
;
312 ops
.mode
= MTD_OPS_RAW
;
318 ret
= mtd_write_oob(mtd
, *ppos
, &ops
);
324 ret
= mtd_write(mtd
, *ppos
, len
, &retlen
, kbuf
);
328 total_retlen
+= retlen
;
340 } /* mtdchar_write */
342 /*======================================================================
344 IOCTL calls for getting device parameters.
346 ======================================================================*/
347 static void mtdchar_erase_callback (struct erase_info
*instr
)
349 wake_up((wait_queue_head_t
*)instr
->priv
);
352 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
354 struct mtd_info
*mtd
= mfi
->mtd
;
358 case MTD_OTP_FACTORY
:
359 if (mtd_read_fact_prot_reg(mtd
, -1, 0, &retlen
, NULL
) ==
363 mfi
->mode
= MTD_FILE_MODE_OTP_FACTORY
;
366 if (mtd_read_user_prot_reg(mtd
, -1, 0, &retlen
, NULL
) ==
370 mfi
->mode
= MTD_FILE_MODE_OTP_USER
;
373 mfi
->mode
= MTD_FILE_MODE_NORMAL
;
382 static int mtdchar_writeoob(struct file
*file
, struct mtd_info
*mtd
,
383 uint64_t start
, uint32_t length
, void __user
*ptr
,
384 uint32_t __user
*retp
)
386 struct mtd_file_info
*mfi
= file
->private_data
;
387 struct mtd_oob_ops ops
;
391 if (!(file
->f_mode
& FMODE_WRITE
))
397 if (!mtd
->_write_oob
)
400 ret
= access_ok(VERIFY_READ
, ptr
, length
) ? 0 : -EFAULT
;
406 ops
.ooboffs
= start
& (mtd
->writesize
- 1);
408 ops
.mode
= (mfi
->mode
== MTD_FILE_MODE_RAW
) ? MTD_OPS_RAW
:
411 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
414 ops
.oobbuf
= memdup_user(ptr
, length
);
415 if (IS_ERR(ops
.oobbuf
))
416 return PTR_ERR(ops
.oobbuf
);
418 start
&= ~((uint64_t)mtd
->writesize
- 1);
419 ret
= mtd_write_oob(mtd
, start
, &ops
);
421 if (ops
.oobretlen
> 0xFFFFFFFFU
)
423 retlen
= ops
.oobretlen
;
424 if (copy_to_user(retp
, &retlen
, sizeof(length
)))
431 static int mtdchar_readoob(struct file
*file
, struct mtd_info
*mtd
,
432 uint64_t start
, uint32_t length
, void __user
*ptr
,
433 uint32_t __user
*retp
)
435 struct mtd_file_info
*mfi
= file
->private_data
;
436 struct mtd_oob_ops ops
;
442 if (!access_ok(VERIFY_WRITE
, ptr
, length
))
446 ops
.ooboffs
= start
& (mtd
->writesize
- 1);
448 ops
.mode
= (mfi
->mode
== MTD_FILE_MODE_RAW
) ? MTD_OPS_RAW
:
451 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
454 ops
.oobbuf
= kmalloc(length
, GFP_KERNEL
);
458 start
&= ~((uint64_t)mtd
->writesize
- 1);
459 ret
= mtd_read_oob(mtd
, start
, &ops
);
461 if (put_user(ops
.oobretlen
, retp
))
463 else if (ops
.oobretlen
&& copy_to_user(ptr
, ops
.oobbuf
,
470 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
471 * data. For our userspace tools it is important to dump areas
473 * For kernel internal usage it also might return -EUCLEAN
474 * to signal the caller that a bitflip has occured and has
475 * been corrected by the ECC algorithm.
477 * Note: currently the standard NAND function, nand_read_oob_std,
478 * does not calculate ECC for the OOB area, so do not rely on
479 * this behavior unless you have replaced it with your own.
481 if (mtd_is_bitflip_or_eccerr(ret
))
488 * Copies (and truncates, if necessary) data from the larger struct,
489 * nand_ecclayout, to the smaller, deprecated layout struct,
490 * nand_ecclayout_user. This is necessary only to support the deprecated
491 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
492 * nand_ecclayout flexibly (i.e. the struct may change size in new
493 * releases without requiring major rewrites).
495 static int shrink_ecclayout(const struct nand_ecclayout
*from
,
496 struct nand_ecclayout_user
*to
)
503 memset(to
, 0, sizeof(*to
));
505 to
->eccbytes
= min((int)from
->eccbytes
, MTD_MAX_ECCPOS_ENTRIES
);
506 for (i
= 0; i
< to
->eccbytes
; i
++)
507 to
->eccpos
[i
] = from
->eccpos
[i
];
509 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
; i
++) {
510 if (from
->oobfree
[i
].length
== 0 &&
511 from
->oobfree
[i
].offset
== 0)
513 to
->oobavail
+= from
->oobfree
[i
].length
;
514 to
->oobfree
[i
] = from
->oobfree
[i
];
520 static int mtdchar_blkpg_ioctl(struct mtd_info
*mtd
,
521 struct blkpg_ioctl_arg __user
*arg
)
523 struct blkpg_ioctl_arg a
;
524 struct blkpg_partition p
;
526 if (!capable(CAP_SYS_ADMIN
))
529 if (copy_from_user(&a
, arg
, sizeof(struct blkpg_ioctl_arg
)))
532 if (copy_from_user(&p
, a
.data
, sizeof(struct blkpg_partition
)))
536 case BLKPG_ADD_PARTITION
:
538 /* Only master mtd device must be used to add partitions */
539 if (mtd_is_partition(mtd
))
542 return mtd_add_partition(mtd
, p
.devname
, p
.start
, p
.length
);
544 case BLKPG_DEL_PARTITION
:
549 return mtd_del_partition(mtd
, p
.pno
);
556 static int mtdchar_write_ioctl(struct mtd_info
*mtd
,
557 struct mtd_write_req __user
*argp
)
559 struct mtd_write_req req
;
560 struct mtd_oob_ops ops
;
561 void __user
*usr_data
, *usr_oob
;
564 if (copy_from_user(&req
, argp
, sizeof(req
)) ||
565 !access_ok(VERIFY_READ
, req
.usr_data
, req
.len
) ||
566 !access_ok(VERIFY_READ
, req
.usr_oob
, req
.ooblen
))
568 if (!mtd
->_write_oob
)
572 ops
.len
= (size_t)req
.len
;
573 ops
.ooblen
= (size_t)req
.ooblen
;
576 usr_data
= (void __user
*)(uintptr_t)req
.usr_data
;
577 usr_oob
= (void __user
*)(uintptr_t)req
.usr_oob
;
580 ops
.datbuf
= memdup_user(usr_data
, ops
.len
);
581 if (IS_ERR(ops
.datbuf
))
582 return PTR_ERR(ops
.datbuf
);
588 ops
.oobbuf
= memdup_user(usr_oob
, ops
.ooblen
);
589 if (IS_ERR(ops
.oobbuf
)) {
591 return PTR_ERR(ops
.oobbuf
);
597 ret
= mtd_write_oob(mtd
, (loff_t
)req
.start
, &ops
);
605 static int mtdchar_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
607 struct mtd_file_info
*mfi
= file
->private_data
;
608 struct mtd_info
*mtd
= mfi
->mtd
;
609 void __user
*argp
= (void __user
*)arg
;
612 struct mtd_info_user info
;
614 pr_debug("MTD_ioctl\n");
616 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
618 if (!access_ok(VERIFY_READ
, argp
, size
))
622 if (!access_ok(VERIFY_WRITE
, argp
, size
))
627 case MEMGETREGIONCOUNT
:
628 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
632 case MEMGETREGIONINFO
:
635 struct mtd_erase_region_info
*kr
;
636 struct region_info_user __user
*ur
= argp
;
638 if (get_user(ur_idx
, &(ur
->regionindex
)))
641 if (ur_idx
>= mtd
->numeraseregions
)
644 kr
= &(mtd
->eraseregions
[ur_idx
]);
646 if (put_user(kr
->offset
, &(ur
->offset
))
647 || put_user(kr
->erasesize
, &(ur
->erasesize
))
648 || put_user(kr
->numblocks
, &(ur
->numblocks
)))
655 memset(&info
, 0, sizeof(info
));
656 info
.type
= mtd
->type
;
657 info
.flags
= mtd
->flags
;
658 info
.size
= mtd
->size
;
659 info
.erasesize
= mtd
->erasesize
;
660 info
.writesize
= mtd
->writesize
;
661 info
.oobsize
= mtd
->oobsize
;
662 /* The below field is obsolete */
664 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
671 struct erase_info
*erase
;
673 if(!(file
->f_mode
& FMODE_WRITE
))
676 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
680 wait_queue_head_t waitq
;
681 DECLARE_WAITQUEUE(wait
, current
);
683 init_waitqueue_head(&waitq
);
685 if (cmd
== MEMERASE64
) {
686 struct erase_info_user64 einfo64
;
688 if (copy_from_user(&einfo64
, argp
,
689 sizeof(struct erase_info_user64
))) {
693 erase
->addr
= einfo64
.start
;
694 erase
->len
= einfo64
.length
;
696 struct erase_info_user einfo32
;
698 if (copy_from_user(&einfo32
, argp
,
699 sizeof(struct erase_info_user
))) {
703 erase
->addr
= einfo32
.start
;
704 erase
->len
= einfo32
.length
;
707 erase
->callback
= mtdchar_erase_callback
;
708 erase
->priv
= (unsigned long)&waitq
;
711 FIXME: Allow INTERRUPTIBLE. Which means
712 not having the wait_queue head on the stack.
714 If the wq_head is on the stack, and we
715 leave because we got interrupted, then the
716 wq_head is no longer there when the
717 callback routine tries to wake us up.
719 ret
= mtd_erase(mtd
, erase
);
721 set_current_state(TASK_UNINTERRUPTIBLE
);
722 add_wait_queue(&waitq
, &wait
);
723 if (erase
->state
!= MTD_ERASE_DONE
&&
724 erase
->state
!= MTD_ERASE_FAILED
)
726 remove_wait_queue(&waitq
, &wait
);
727 set_current_state(TASK_RUNNING
);
729 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
738 struct mtd_oob_buf buf
;
739 struct mtd_oob_buf __user
*buf_user
= argp
;
741 /* NOTE: writes return length to buf_user->length */
742 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
745 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
, buf
.length
,
746 buf
.ptr
, &buf_user
->length
);
752 struct mtd_oob_buf buf
;
753 struct mtd_oob_buf __user
*buf_user
= argp
;
755 /* NOTE: writes return length to buf_user->start */
756 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
759 ret
= mtdchar_readoob(file
, mtd
, buf
.start
, buf
.length
,
760 buf
.ptr
, &buf_user
->start
);
766 struct mtd_oob_buf64 buf
;
767 struct mtd_oob_buf64 __user
*buf_user
= argp
;
769 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
772 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
, buf
.length
,
773 (void __user
*)(uintptr_t)buf
.usr_ptr
,
780 struct mtd_oob_buf64 buf
;
781 struct mtd_oob_buf64 __user
*buf_user
= argp
;
783 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
786 ret
= mtdchar_readoob(file
, mtd
, buf
.start
, buf
.length
,
787 (void __user
*)(uintptr_t)buf
.usr_ptr
,
794 ret
= mtdchar_write_ioctl(mtd
,
795 (struct mtd_write_req __user
*)arg
);
801 struct erase_info_user einfo
;
803 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
806 ret
= mtd_lock(mtd
, einfo
.start
, einfo
.length
);
812 struct erase_info_user einfo
;
814 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
817 ret
= mtd_unlock(mtd
, einfo
.start
, einfo
.length
);
823 struct erase_info_user einfo
;
825 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
828 ret
= mtd_is_locked(mtd
, einfo
.start
, einfo
.length
);
832 /* Legacy interface */
835 struct nand_oobinfo oi
;
839 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
842 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
843 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
844 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
846 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
848 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
857 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
859 return mtd_block_isbad(mtd
, offs
);
867 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
869 return mtd_block_markbad(mtd
, offs
);
876 if (copy_from_user(&mode
, argp
, sizeof(int)))
879 mfi
->mode
= MTD_FILE_MODE_NORMAL
;
881 ret
= otp_select_filemode(mfi
, mode
);
887 case OTPGETREGIONCOUNT
:
888 case OTPGETREGIONINFO
:
890 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
894 case MTD_FILE_MODE_OTP_FACTORY
:
895 ret
= mtd_get_fact_prot_info(mtd
, buf
, 4096);
897 case MTD_FILE_MODE_OTP_USER
:
898 ret
= mtd_get_user_prot_info(mtd
, buf
, 4096);
905 if (cmd
== OTPGETREGIONCOUNT
) {
906 int nbr
= ret
/ sizeof(struct otp_info
);
907 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
909 ret
= copy_to_user(argp
, buf
, ret
);
919 struct otp_info oinfo
;
921 if (mfi
->mode
!= MTD_FILE_MODE_OTP_USER
)
923 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
925 ret
= mtd_lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
929 /* This ioctl is being deprecated - it truncates the ECC layout */
932 struct nand_ecclayout_user
*usrlay
;
937 usrlay
= kmalloc(sizeof(*usrlay
), GFP_KERNEL
);
941 shrink_ecclayout(mtd
->ecclayout
, usrlay
);
943 if (copy_to_user(argp
, usrlay
, sizeof(*usrlay
)))
951 if (copy_to_user(argp
, &mtd
->ecc_stats
,
952 sizeof(struct mtd_ecc_stats
)))
962 case MTD_FILE_MODE_OTP_FACTORY
:
963 case MTD_FILE_MODE_OTP_USER
:
964 ret
= otp_select_filemode(mfi
, arg
);
967 case MTD_FILE_MODE_RAW
:
968 if (!mtd_has_oob(mtd
))
972 case MTD_FILE_MODE_NORMAL
:
983 ret
= mtdchar_blkpg_ioctl(mtd
,
984 (struct blkpg_ioctl_arg __user
*)arg
);
990 /* No reread partition feature. Just return ok */
1000 } /* memory_ioctl */
1002 static long mtdchar_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
1006 mutex_lock(&mtd_mutex
);
1007 ret
= mtdchar_ioctl(file
, cmd
, arg
);
1008 mutex_unlock(&mtd_mutex
);
1013 #ifdef CONFIG_COMPAT
1015 struct mtd_oob_buf32
{
1018 compat_caddr_t ptr
; /* unsigned char* */
1021 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1022 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1024 static long mtdchar_compat_ioctl(struct file
*file
, unsigned int cmd
,
1027 struct mtd_file_info
*mfi
= file
->private_data
;
1028 struct mtd_info
*mtd
= mfi
->mtd
;
1029 void __user
*argp
= compat_ptr(arg
);
1032 mutex_lock(&mtd_mutex
);
1037 struct mtd_oob_buf32 buf
;
1038 struct mtd_oob_buf32 __user
*buf_user
= argp
;
1040 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
1043 ret
= mtdchar_writeoob(file
, mtd
, buf
.start
,
1044 buf
.length
, compat_ptr(buf
.ptr
),
1051 struct mtd_oob_buf32 buf
;
1052 struct mtd_oob_buf32 __user
*buf_user
= argp
;
1054 /* NOTE: writes return length to buf->start */
1055 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
1058 ret
= mtdchar_readoob(file
, mtd
, buf
.start
,
1059 buf
.length
, compat_ptr(buf
.ptr
),
1064 ret
= mtdchar_ioctl(file
, cmd
, (unsigned long)argp
);
1067 mutex_unlock(&mtd_mutex
);
1072 #endif /* CONFIG_COMPAT */
1075 * try to determine where a shared mapping can be made
1076 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1080 static unsigned long mtdchar_get_unmapped_area(struct file
*file
,
1083 unsigned long pgoff
,
1084 unsigned long flags
)
1086 struct mtd_file_info
*mfi
= file
->private_data
;
1087 struct mtd_info
*mtd
= mfi
->mtd
;
1088 unsigned long offset
;
1092 return (unsigned long) -EINVAL
;
1094 if (len
> mtd
->size
|| pgoff
>= (mtd
->size
>> PAGE_SHIFT
))
1095 return (unsigned long) -EINVAL
;
1097 offset
= pgoff
<< PAGE_SHIFT
;
1098 if (offset
> mtd
->size
- len
)
1099 return (unsigned long) -EINVAL
;
1101 ret
= mtd_get_unmapped_area(mtd
, len
, offset
, flags
);
1102 return ret
== -EOPNOTSUPP
? -ENOSYS
: ret
;
1107 * set up a mapping for shared memory segments
1109 static int mtdchar_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1112 struct mtd_file_info
*mfi
= file
->private_data
;
1113 struct mtd_info
*mtd
= mfi
->mtd
;
1114 struct map_info
*map
= mtd
->priv
;
1116 /* This is broken because it assumes the MTD device is map-based
1117 and that mtd->priv is a valid struct map_info. It should be
1118 replaced with something that uses the mtd_get_unmapped_area()
1119 operation properly. */
1120 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
1121 #ifdef pgprot_noncached
1122 if (file
->f_flags
& O_DSYNC
|| map
->phys
>= __pa(high_memory
))
1123 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1125 return vm_iomap_memory(vma
, map
->phys
, map
->size
);
1129 return vma
->vm_flags
& VM_SHARED
? 0 : -ENOSYS
;
1133 static const struct file_operations mtd_fops
= {
1134 .owner
= THIS_MODULE
,
1135 .llseek
= mtdchar_lseek
,
1136 .read
= mtdchar_read
,
1137 .write
= mtdchar_write
,
1138 .unlocked_ioctl
= mtdchar_unlocked_ioctl
,
1139 #ifdef CONFIG_COMPAT
1140 .compat_ioctl
= mtdchar_compat_ioctl
,
1142 .open
= mtdchar_open
,
1143 .release
= mtdchar_close
,
1144 .mmap
= mtdchar_mmap
,
1146 .get_unmapped_area
= mtdchar_get_unmapped_area
,
1150 static const struct super_operations mtd_ops
= {
1151 .drop_inode
= generic_delete_inode
,
1152 .statfs
= simple_statfs
,
1155 static struct dentry
*mtd_inodefs_mount(struct file_system_type
*fs_type
,
1156 int flags
, const char *dev_name
, void *data
)
1158 return mount_pseudo(fs_type
, "mtd_inode:", &mtd_ops
, NULL
, MTD_INODE_FS_MAGIC
);
1161 static struct file_system_type mtd_inodefs_type
= {
1162 .name
= "mtd_inodefs",
1163 .mount
= mtd_inodefs_mount
,
1164 .kill_sb
= kill_anon_super
,
1166 MODULE_ALIAS_FS("mtd_inodefs");
1168 int __init
init_mtdchar(void)
1172 ret
= __register_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
,
1175 pr_err("Can't allocate major number %d for MTD\n",
1180 ret
= register_filesystem(&mtd_inodefs_type
);
1182 pr_err("Can't register mtd_inodefs filesystem, error %d\n",
1184 goto err_unregister_chdev
;
1189 err_unregister_chdev
:
1190 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1194 void __exit
cleanup_mtdchar(void)
1196 unregister_filesystem(&mtd_inodefs_type
);
1197 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1200 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);