2 * Character-device access to raw MTD devices.
6 #include <linux/device.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/backing-dev.h>
17 #include <linux/compat.h>
18 #include <linux/mount.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/compatmac.h>
23 #include <asm/uaccess.h>
25 #define MTD_INODE_FS_MAGIC 0x11307854
26 static struct vfsmount
*mtd_inode_mnt __read_mostly
;
29 * Data structure to hold the pointer to the mtd device as well
30 * as mode information ofr various use cases.
32 struct mtd_file_info
{
35 enum mtd_file_modes mode
;
38 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
40 struct mtd_file_info
*mfi
= file
->private_data
;
41 struct mtd_info
*mtd
= mfi
->mtd
;
47 offset
+= file
->f_pos
;
56 if (offset
>= 0 && offset
<= mtd
->size
)
57 return file
->f_pos
= offset
;
64 static int mtd_open(struct inode
*inode
, struct file
*file
)
66 int minor
= iminor(inode
);
67 int devnum
= minor
>> 1;
70 struct mtd_file_info
*mfi
;
71 struct inode
*mtd_ino
;
73 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
75 /* You can't open the RO devices RW */
76 if ((file
->f_mode
& FMODE_WRITE
) && (minor
& 1))
80 mtd
= get_mtd_device(NULL
, devnum
);
87 if (mtd
->type
== MTD_ABSENT
) {
93 mtd_ino
= iget_locked(mtd_inode_mnt
->mnt_sb
, devnum
);
99 if (mtd_ino
->i_state
& I_NEW
) {
100 mtd_ino
->i_private
= mtd
;
101 mtd_ino
->i_mode
= S_IFCHR
;
102 mtd_ino
->i_data
.backing_dev_info
= mtd
->backing_dev_info
;
103 unlock_new_inode(mtd_ino
);
105 file
->f_mapping
= mtd_ino
->i_mapping
;
107 /* You can't open it RW if it's not a writeable device */
108 if ((file
->f_mode
& FMODE_WRITE
) && !(mtd
->flags
& MTD_WRITEABLE
)) {
115 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
124 file
->private_data
= mfi
;
131 /*====================================================================*/
133 static int mtd_close(struct inode
*inode
, struct file
*file
)
135 struct mtd_file_info
*mfi
= file
->private_data
;
136 struct mtd_info
*mtd
= mfi
->mtd
;
138 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
140 /* Only sync if opened RW */
141 if ((file
->f_mode
& FMODE_WRITE
) && mtd
->sync
)
147 file
->private_data
= NULL
;
153 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
154 userspace buffer down and use it directly with readv/writev.
156 #define MAX_KMALLOC_SIZE 0x20000
158 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
160 struct mtd_file_info
*mfi
= file
->private_data
;
161 struct mtd_info
*mtd
= mfi
->mtd
;
163 size_t total_retlen
=0;
168 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
170 if (*ppos
+ count
> mtd
->size
)
171 count
= mtd
->size
- *ppos
;
176 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
177 and pass them directly to the MTD functions */
179 if (count
> MAX_KMALLOC_SIZE
)
180 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
182 kbuf
=kmalloc(count
, GFP_KERNEL
);
189 if (count
> MAX_KMALLOC_SIZE
)
190 len
= MAX_KMALLOC_SIZE
;
195 case MTD_MODE_OTP_FACTORY
:
196 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
198 case MTD_MODE_OTP_USER
:
199 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
203 struct mtd_oob_ops ops
;
205 ops
.mode
= MTD_OOB_RAW
;
210 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
215 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
217 /* Nand returns -EBADMSG on ecc errors, but it returns
218 * the data. For our userspace tools it is important
219 * to dump areas with ecc errors !
220 * For kernel internal usage it also might return -EUCLEAN
221 * to signal the caller that a bitflip has occured and has
222 * been corrected by the ECC algorithm.
223 * Userspace software which accesses NAND this way
224 * must be aware of the fact that it deals with NAND
226 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
228 if (copy_to_user(buf
, kbuf
, retlen
)) {
233 total_retlen
+= retlen
;
251 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
253 struct mtd_file_info
*mfi
= file
->private_data
;
254 struct mtd_info
*mtd
= mfi
->mtd
;
257 size_t total_retlen
=0;
261 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
263 if (*ppos
== mtd
->size
)
266 if (*ppos
+ count
> mtd
->size
)
267 count
= mtd
->size
- *ppos
;
272 if (count
> MAX_KMALLOC_SIZE
)
273 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
275 kbuf
=kmalloc(count
, GFP_KERNEL
);
282 if (count
> MAX_KMALLOC_SIZE
)
283 len
= MAX_KMALLOC_SIZE
;
287 if (copy_from_user(kbuf
, buf
, len
)) {
293 case MTD_MODE_OTP_FACTORY
:
296 case MTD_MODE_OTP_USER
:
297 if (!mtd
->write_user_prot_reg
) {
301 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
306 struct mtd_oob_ops ops
;
308 ops
.mode
= MTD_OOB_RAW
;
313 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
319 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
323 total_retlen
+= retlen
;
337 /*======================================================================
339 IOCTL calls for getting device parameters.
341 ======================================================================*/
342 static void mtdchar_erase_callback (struct erase_info
*instr
)
344 wake_up((wait_queue_head_t
*)instr
->priv
);
347 #ifdef CONFIG_HAVE_MTD_OTP
348 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
350 struct mtd_info
*mtd
= mfi
->mtd
;
354 case MTD_OTP_FACTORY
:
355 if (!mtd
->read_fact_prot_reg
)
358 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
361 if (!mtd
->read_fact_prot_reg
)
364 mfi
->mode
= MTD_MODE_OTP_USER
;
374 # define otp_select_filemode(f,m) -EOPNOTSUPP
377 static int mtd_do_writeoob(struct file
*file
, struct mtd_info
*mtd
,
378 uint64_t start
, uint32_t length
, void __user
*ptr
,
379 uint32_t __user
*retp
)
381 struct mtd_oob_ops ops
;
385 if (!(file
->f_mode
& FMODE_WRITE
))
394 ret
= access_ok(VERIFY_READ
, ptr
, length
) ? 0 : -EFAULT
;
400 ops
.ooboffs
= start
& (mtd
->oobsize
- 1);
402 ops
.mode
= MTD_OOB_PLACE
;
404 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
407 ops
.oobbuf
= kmalloc(length
, GFP_KERNEL
);
411 if (copy_from_user(ops
.oobbuf
, ptr
, length
)) {
416 start
&= ~((uint64_t)mtd
->oobsize
- 1);
417 ret
= mtd
->write_oob(mtd
, start
, &ops
);
419 if (ops
.oobretlen
> 0xFFFFFFFFU
)
421 retlen
= ops
.oobretlen
;
422 if (copy_to_user(retp
, &retlen
, sizeof(length
)))
429 static int mtd_do_readoob(struct mtd_info
*mtd
, uint64_t start
,
430 uint32_t length
, void __user
*ptr
, uint32_t __user
*retp
)
432 struct mtd_oob_ops ops
;
441 ret
= access_ok(VERIFY_WRITE
, ptr
,
442 length
) ? 0 : -EFAULT
;
447 ops
.ooboffs
= start
& (mtd
->oobsize
- 1);
449 ops
.mode
= MTD_OOB_PLACE
;
451 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
454 ops
.oobbuf
= kmalloc(length
, GFP_KERNEL
);
458 start
&= ~((uint64_t)mtd
->oobsize
- 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
,
471 static int mtd_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
473 struct mtd_file_info
*mfi
= file
->private_data
;
474 struct mtd_info
*mtd
= mfi
->mtd
;
475 void __user
*argp
= (void __user
*)arg
;
478 struct mtd_info_user info
;
480 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
482 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
484 if (!access_ok(VERIFY_READ
, argp
, size
))
488 if (!access_ok(VERIFY_WRITE
, argp
, size
))
493 case MEMGETREGIONCOUNT
:
494 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
498 case MEMGETREGIONINFO
:
501 struct mtd_erase_region_info
*kr
;
502 struct region_info_user __user
*ur
= argp
;
504 if (get_user(ur_idx
, &(ur
->regionindex
)))
507 kr
= &(mtd
->eraseregions
[ur_idx
]);
509 if (put_user(kr
->offset
, &(ur
->offset
))
510 || put_user(kr
->erasesize
, &(ur
->erasesize
))
511 || put_user(kr
->numblocks
, &(ur
->numblocks
)))
518 info
.type
= mtd
->type
;
519 info
.flags
= mtd
->flags
;
520 info
.size
= mtd
->size
;
521 info
.erasesize
= mtd
->erasesize
;
522 info
.writesize
= mtd
->writesize
;
523 info
.oobsize
= mtd
->oobsize
;
524 /* The below fields are obsolete */
527 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
534 struct erase_info
*erase
;
536 if(!(file
->f_mode
& FMODE_WRITE
))
539 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
543 wait_queue_head_t waitq
;
544 DECLARE_WAITQUEUE(wait
, current
);
546 init_waitqueue_head(&waitq
);
548 if (cmd
== MEMERASE64
) {
549 struct erase_info_user64 einfo64
;
551 if (copy_from_user(&einfo64
, argp
,
552 sizeof(struct erase_info_user64
))) {
556 erase
->addr
= einfo64
.start
;
557 erase
->len
= einfo64
.length
;
559 struct erase_info_user einfo32
;
561 if (copy_from_user(&einfo32
, argp
,
562 sizeof(struct erase_info_user
))) {
566 erase
->addr
= einfo32
.start
;
567 erase
->len
= einfo32
.length
;
570 erase
->callback
= mtdchar_erase_callback
;
571 erase
->priv
= (unsigned long)&waitq
;
574 FIXME: Allow INTERRUPTIBLE. Which means
575 not having the wait_queue head on the stack.
577 If the wq_head is on the stack, and we
578 leave because we got interrupted, then the
579 wq_head is no longer there when the
580 callback routine tries to wake us up.
582 ret
= mtd
->erase(mtd
, erase
);
584 set_current_state(TASK_UNINTERRUPTIBLE
);
585 add_wait_queue(&waitq
, &wait
);
586 if (erase
->state
!= MTD_ERASE_DONE
&&
587 erase
->state
!= MTD_ERASE_FAILED
)
589 remove_wait_queue(&waitq
, &wait
);
590 set_current_state(TASK_RUNNING
);
592 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
601 struct mtd_oob_buf buf
;
602 struct mtd_oob_buf __user
*buf_user
= argp
;
604 /* NOTE: writes return length to buf_user->length */
605 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
608 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
, buf
.length
,
609 buf
.ptr
, &buf_user
->length
);
615 struct mtd_oob_buf buf
;
616 struct mtd_oob_buf __user
*buf_user
= argp
;
618 /* NOTE: writes return length to buf_user->start */
619 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
622 ret
= mtd_do_readoob(mtd
, buf
.start
, buf
.length
,
623 buf
.ptr
, &buf_user
->start
);
629 struct mtd_oob_buf64 buf
;
630 struct mtd_oob_buf64 __user
*buf_user
= argp
;
632 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
635 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
, buf
.length
,
636 (void __user
*)(uintptr_t)buf
.usr_ptr
,
643 struct mtd_oob_buf64 buf
;
644 struct mtd_oob_buf64 __user
*buf_user
= argp
;
646 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
649 ret
= mtd_do_readoob(mtd
, buf
.start
, buf
.length
,
650 (void __user
*)(uintptr_t)buf
.usr_ptr
,
657 struct erase_info_user einfo
;
659 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
665 ret
= mtd
->lock(mtd
, einfo
.start
, einfo
.length
);
671 struct erase_info_user einfo
;
673 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
679 ret
= mtd
->unlock(mtd
, einfo
.start
, einfo
.length
);
683 /* Legacy interface */
686 struct nand_oobinfo oi
;
690 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
693 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
694 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
695 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
697 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
699 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
708 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
710 if (!mtd
->block_isbad
)
713 return mtd
->block_isbad(mtd
, offs
);
721 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
723 if (!mtd
->block_markbad
)
726 return mtd
->block_markbad(mtd
, offs
);
730 #ifdef CONFIG_HAVE_MTD_OTP
734 if (copy_from_user(&mode
, argp
, sizeof(int)))
737 mfi
->mode
= MTD_MODE_NORMAL
;
739 ret
= otp_select_filemode(mfi
, mode
);
745 case OTPGETREGIONCOUNT
:
746 case OTPGETREGIONINFO
:
748 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
753 case MTD_MODE_OTP_FACTORY
:
754 if (mtd
->get_fact_prot_info
)
755 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
757 case MTD_MODE_OTP_USER
:
758 if (mtd
->get_user_prot_info
)
759 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
765 if (cmd
== OTPGETREGIONCOUNT
) {
766 int nbr
= ret
/ sizeof(struct otp_info
);
767 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
769 ret
= copy_to_user(argp
, buf
, ret
);
779 struct otp_info oinfo
;
781 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
783 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
785 if (!mtd
->lock_user_prot_reg
)
787 ret
= mtd
->lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
797 if (copy_to_user(argp
, mtd
->ecclayout
,
798 sizeof(struct nand_ecclayout
)))
805 if (copy_to_user(argp
, &mtd
->ecc_stats
,
806 sizeof(struct mtd_ecc_stats
)))
816 case MTD_MODE_OTP_FACTORY
:
817 case MTD_MODE_OTP_USER
:
818 ret
= otp_select_filemode(mfi
, arg
);
822 if (!mtd
->read_oob
|| !mtd
->write_oob
)
826 case MTD_MODE_NORMAL
:
842 static long mtd_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
847 ret
= mtd_ioctl(file
, cmd
, arg
);
855 struct mtd_oob_buf32
{
858 compat_caddr_t ptr
; /* unsigned char* */
861 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
862 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
864 static long mtd_compat_ioctl(struct file
*file
, unsigned int cmd
,
867 struct mtd_file_info
*mfi
= file
->private_data
;
868 struct mtd_info
*mtd
= mfi
->mtd
;
869 void __user
*argp
= compat_ptr(arg
);
877 struct mtd_oob_buf32 buf
;
878 struct mtd_oob_buf32 __user
*buf_user
= argp
;
880 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
883 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
,
884 buf
.length
, compat_ptr(buf
.ptr
),
891 struct mtd_oob_buf32 buf
;
892 struct mtd_oob_buf32 __user
*buf_user
= argp
;
894 /* NOTE: writes return length to buf->start */
895 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
898 ret
= mtd_do_readoob(mtd
, buf
.start
,
899 buf
.length
, compat_ptr(buf
.ptr
),
904 ret
= mtd_ioctl(file
, cmd
, (unsigned long)argp
);
912 #endif /* CONFIG_COMPAT */
915 * try to determine where a shared mapping can be made
916 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
920 static unsigned long mtd_get_unmapped_area(struct file
*file
,
926 struct mtd_file_info
*mfi
= file
->private_data
;
927 struct mtd_info
*mtd
= mfi
->mtd
;
929 if (mtd
->get_unmapped_area
) {
930 unsigned long offset
;
933 return (unsigned long) -EINVAL
;
935 if (len
> mtd
->size
|| pgoff
>= (mtd
->size
>> PAGE_SHIFT
))
936 return (unsigned long) -EINVAL
;
938 offset
= pgoff
<< PAGE_SHIFT
;
939 if (offset
> mtd
->size
- len
)
940 return (unsigned long) -EINVAL
;
942 return mtd
->get_unmapped_area(mtd
, len
, offset
, flags
);
945 /* can't map directly */
946 return (unsigned long) -ENOSYS
;
951 * set up a mapping for shared memory segments
953 static int mtd_mmap(struct file
*file
, struct vm_area_struct
*vma
)
956 struct mtd_file_info
*mfi
= file
->private_data
;
957 struct mtd_info
*mtd
= mfi
->mtd
;
959 if (mtd
->type
== MTD_RAM
|| mtd
->type
== MTD_ROM
)
963 return vma
->vm_flags
& VM_SHARED
? 0 : -ENOSYS
;
967 static const struct file_operations mtd_fops
= {
968 .owner
= THIS_MODULE
,
972 .unlocked_ioctl
= mtd_unlocked_ioctl
,
974 .compat_ioctl
= mtd_compat_ioctl
,
977 .release
= mtd_close
,
980 .get_unmapped_area
= mtd_get_unmapped_area
,
984 static int mtd_inodefs_get_sb(struct file_system_type
*fs_type
, int flags
,
985 const char *dev_name
, void *data
,
986 struct vfsmount
*mnt
)
988 return get_sb_pseudo(fs_type
, "mtd_inode:", NULL
, MTD_INODE_FS_MAGIC
,
992 static struct file_system_type mtd_inodefs_type
= {
993 .name
= "mtd_inodefs",
994 .get_sb
= mtd_inodefs_get_sb
,
995 .kill_sb
= kill_anon_super
,
998 static void mtdchar_notify_add(struct mtd_info
*mtd
)
1002 static void mtdchar_notify_remove(struct mtd_info
*mtd
)
1004 struct inode
*mtd_ino
= ilookup(mtd_inode_mnt
->mnt_sb
, mtd
->index
);
1007 /* Destroy the inode if it exists */
1008 mtd_ino
->i_nlink
= 0;
1013 static struct mtd_notifier mtdchar_notifier
= {
1014 .add
= mtdchar_notify_add
,
1015 .remove
= mtdchar_notify_remove
,
1018 static int __init
init_mtdchar(void)
1022 ret
= __register_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
,
1025 pr_notice("Can't allocate major number %d for "
1026 "Memory Technology Devices.\n", MTD_CHAR_MAJOR
);
1030 ret
= register_filesystem(&mtd_inodefs_type
);
1032 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret
);
1033 goto err_unregister_chdev
;
1036 mtd_inode_mnt
= kern_mount(&mtd_inodefs_type
);
1037 if (IS_ERR(mtd_inode_mnt
)) {
1038 ret
= PTR_ERR(mtd_inode_mnt
);
1039 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret
);
1040 goto err_unregister_filesystem
;
1042 register_mtd_user(&mtdchar_notifier
);
1046 err_unregister_filesystem
:
1047 unregister_filesystem(&mtd_inodefs_type
);
1048 err_unregister_chdev
:
1049 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1053 static void __exit
cleanup_mtdchar(void)
1055 unregister_mtd_user(&mtdchar_notifier
);
1056 mntput(mtd_inode_mnt
);
1057 unregister_filesystem(&mtd_inodefs_type
);
1058 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1061 module_init(init_mtdchar
);
1062 module_exit(cleanup_mtdchar
);
1064 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);
1066 MODULE_LICENSE("GPL");
1067 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1068 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
1069 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);