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/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/compat.h>
32 #include <linux/mount.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/map.h>
37 #include <asm/uaccess.h>
39 #define MTD_INODE_FS_MAGIC 0x11307854
40 static struct vfsmount
*mtd_inode_mnt __read_mostly
;
43 * Data structure to hold the pointer to the mtd device as well
44 * as mode information ofr various use cases.
46 struct mtd_file_info
{
49 enum mtd_file_modes mode
;
52 static loff_t
mtd_lseek (struct file
*file
, loff_t offset
, int orig
)
54 struct mtd_file_info
*mfi
= file
->private_data
;
55 struct mtd_info
*mtd
= mfi
->mtd
;
61 offset
+= file
->f_pos
;
70 if (offset
>= 0 && offset
<= mtd
->size
)
71 return file
->f_pos
= offset
;
78 static int mtd_open(struct inode
*inode
, struct file
*file
)
80 int minor
= iminor(inode
);
81 int devnum
= minor
>> 1;
84 struct mtd_file_info
*mfi
;
85 struct inode
*mtd_ino
;
87 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_open\n");
89 /* You can't open the RO devices RW */
90 if ((file
->f_mode
& FMODE_WRITE
) && (minor
& 1))
94 mtd
= get_mtd_device(NULL
, devnum
);
101 if (mtd
->type
== MTD_ABSENT
) {
107 mtd_ino
= iget_locked(mtd_inode_mnt
->mnt_sb
, devnum
);
113 if (mtd_ino
->i_state
& I_NEW
) {
114 mtd_ino
->i_private
= mtd
;
115 mtd_ino
->i_mode
= S_IFCHR
;
116 mtd_ino
->i_data
.backing_dev_info
= mtd
->backing_dev_info
;
117 unlock_new_inode(mtd_ino
);
119 file
->f_mapping
= mtd_ino
->i_mapping
;
121 /* You can't open it RW if it's not a writeable device */
122 if ((file
->f_mode
& FMODE_WRITE
) && !(mtd
->flags
& MTD_WRITEABLE
)) {
129 mfi
= kzalloc(sizeof(*mfi
), GFP_KERNEL
);
138 file
->private_data
= mfi
;
145 /*====================================================================*/
147 static int mtd_close(struct inode
*inode
, struct file
*file
)
149 struct mtd_file_info
*mfi
= file
->private_data
;
150 struct mtd_info
*mtd
= mfi
->mtd
;
152 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_close\n");
154 /* Only sync if opened RW */
155 if ((file
->f_mode
& FMODE_WRITE
) && mtd
->sync
)
161 file
->private_data
= NULL
;
167 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
168 userspace buffer down and use it directly with readv/writev.
170 #define MAX_KMALLOC_SIZE 0x20000
172 static ssize_t
mtd_read(struct file
*file
, char __user
*buf
, size_t count
,loff_t
*ppos
)
174 struct mtd_file_info
*mfi
= file
->private_data
;
175 struct mtd_info
*mtd
= mfi
->mtd
;
177 size_t total_retlen
=0;
182 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_read\n");
184 if (*ppos
+ count
> mtd
->size
)
185 count
= mtd
->size
- *ppos
;
190 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
191 and pass them directly to the MTD functions */
193 if (count
> MAX_KMALLOC_SIZE
)
194 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
196 kbuf
=kmalloc(count
, GFP_KERNEL
);
203 if (count
> MAX_KMALLOC_SIZE
)
204 len
= MAX_KMALLOC_SIZE
;
209 case MTD_MODE_OTP_FACTORY
:
210 ret
= mtd
->read_fact_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
212 case MTD_MODE_OTP_USER
:
213 ret
= mtd
->read_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
217 struct mtd_oob_ops ops
;
219 ops
.mode
= MTD_OOB_RAW
;
224 ret
= mtd
->read_oob(mtd
, *ppos
, &ops
);
229 ret
= mtd
->read(mtd
, *ppos
, len
, &retlen
, kbuf
);
231 /* Nand returns -EBADMSG on ecc errors, but it returns
232 * the data. For our userspace tools it is important
233 * to dump areas with ecc errors !
234 * For kernel internal usage it also might return -EUCLEAN
235 * to signal the caller that a bitflip has occured and has
236 * been corrected by the ECC algorithm.
237 * Userspace software which accesses NAND this way
238 * must be aware of the fact that it deals with NAND
240 if (!ret
|| (ret
== -EUCLEAN
) || (ret
== -EBADMSG
)) {
242 if (copy_to_user(buf
, kbuf
, retlen
)) {
247 total_retlen
+= retlen
;
265 static ssize_t
mtd_write(struct file
*file
, const char __user
*buf
, size_t count
,loff_t
*ppos
)
267 struct mtd_file_info
*mfi
= file
->private_data
;
268 struct mtd_info
*mtd
= mfi
->mtd
;
271 size_t total_retlen
=0;
275 DEBUG(MTD_DEBUG_LEVEL0
,"MTD_write\n");
277 if (*ppos
== mtd
->size
)
280 if (*ppos
+ count
> mtd
->size
)
281 count
= mtd
->size
- *ppos
;
286 if (count
> MAX_KMALLOC_SIZE
)
287 kbuf
=kmalloc(MAX_KMALLOC_SIZE
, GFP_KERNEL
);
289 kbuf
=kmalloc(count
, GFP_KERNEL
);
296 if (count
> MAX_KMALLOC_SIZE
)
297 len
= MAX_KMALLOC_SIZE
;
301 if (copy_from_user(kbuf
, buf
, len
)) {
307 case MTD_MODE_OTP_FACTORY
:
310 case MTD_MODE_OTP_USER
:
311 if (!mtd
->write_user_prot_reg
) {
315 ret
= mtd
->write_user_prot_reg(mtd
, *ppos
, len
, &retlen
, kbuf
);
320 struct mtd_oob_ops ops
;
322 ops
.mode
= MTD_OOB_RAW
;
327 ret
= mtd
->write_oob(mtd
, *ppos
, &ops
);
333 ret
= (*(mtd
->write
))(mtd
, *ppos
, len
, &retlen
, kbuf
);
337 total_retlen
+= retlen
;
351 /*======================================================================
353 IOCTL calls for getting device parameters.
355 ======================================================================*/
356 static void mtdchar_erase_callback (struct erase_info
*instr
)
358 wake_up((wait_queue_head_t
*)instr
->priv
);
361 #ifdef CONFIG_HAVE_MTD_OTP
362 static int otp_select_filemode(struct mtd_file_info
*mfi
, int mode
)
364 struct mtd_info
*mtd
= mfi
->mtd
;
368 case MTD_OTP_FACTORY
:
369 if (!mtd
->read_fact_prot_reg
)
372 mfi
->mode
= MTD_MODE_OTP_FACTORY
;
375 if (!mtd
->read_fact_prot_reg
)
378 mfi
->mode
= MTD_MODE_OTP_USER
;
388 # define otp_select_filemode(f,m) -EOPNOTSUPP
391 static int mtd_do_writeoob(struct file
*file
, struct mtd_info
*mtd
,
392 uint64_t start
, uint32_t length
, void __user
*ptr
,
393 uint32_t __user
*retp
)
395 struct mtd_oob_ops ops
;
399 if (!(file
->f_mode
& FMODE_WRITE
))
408 ret
= access_ok(VERIFY_READ
, ptr
, length
) ? 0 : -EFAULT
;
414 ops
.ooboffs
= start
& (mtd
->oobsize
- 1);
416 ops
.mode
= MTD_OOB_PLACE
;
418 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
421 ops
.oobbuf
= memdup_user(ptr
, length
);
422 if (IS_ERR(ops
.oobbuf
))
423 return PTR_ERR(ops
.oobbuf
);
425 start
&= ~((uint64_t)mtd
->oobsize
- 1);
426 ret
= mtd
->write_oob(mtd
, start
, &ops
);
428 if (ops
.oobretlen
> 0xFFFFFFFFU
)
430 retlen
= ops
.oobretlen
;
431 if (copy_to_user(retp
, &retlen
, sizeof(length
)))
438 static int mtd_do_readoob(struct mtd_info
*mtd
, uint64_t start
,
439 uint32_t length
, void __user
*ptr
, uint32_t __user
*retp
)
441 struct mtd_oob_ops ops
;
450 ret
= access_ok(VERIFY_WRITE
, ptr
,
451 length
) ? 0 : -EFAULT
;
456 ops
.ooboffs
= start
& (mtd
->oobsize
- 1);
458 ops
.mode
= MTD_OOB_PLACE
;
460 if (ops
.ooboffs
&& ops
.ooblen
> (mtd
->oobsize
- ops
.ooboffs
))
463 ops
.oobbuf
= kmalloc(length
, GFP_KERNEL
);
467 start
&= ~((uint64_t)mtd
->oobsize
- 1);
468 ret
= mtd
->read_oob(mtd
, start
, &ops
);
470 if (put_user(ops
.oobretlen
, retp
))
472 else if (ops
.oobretlen
&& copy_to_user(ptr
, ops
.oobbuf
,
481 * Copies (and truncates, if necessary) data from the larger struct,
482 * nand_ecclayout, to the smaller, deprecated layout struct,
483 * nand_ecclayout_user. This is necessary only to suppport the deprecated
484 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
485 * nand_ecclayout flexibly (i.e. the struct may change size in new
486 * releases without requiring major rewrites).
488 static int shrink_ecclayout(const struct nand_ecclayout
*from
,
489 struct nand_ecclayout_user
*to
)
496 memset(to
, 0, sizeof(*to
));
498 to
->eccbytes
= min((int)from
->eccbytes
, MTD_MAX_ECCPOS_ENTRIES_OLD
);
499 for (i
= 0; i
< to
->eccbytes
; i
++)
500 to
->eccpos
[i
] = from
->eccpos
[i
];
502 for (i
= 0; i
< MTD_MAX_OOBFREE_ENTRIES
; i
++) {
503 if (from
->oobfree
[i
].length
== 0 &&
504 from
->oobfree
[i
].offset
== 0)
506 to
->oobavail
+= from
->oobfree
[i
].length
;
507 to
->oobfree
[i
] = from
->oobfree
[i
];
513 static int mtd_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
515 struct mtd_file_info
*mfi
= file
->private_data
;
516 struct mtd_info
*mtd
= mfi
->mtd
;
517 void __user
*argp
= (void __user
*)arg
;
520 struct mtd_info_user info
;
522 DEBUG(MTD_DEBUG_LEVEL0
, "MTD_ioctl\n");
524 size
= (cmd
& IOCSIZE_MASK
) >> IOCSIZE_SHIFT
;
526 if (!access_ok(VERIFY_READ
, argp
, size
))
530 if (!access_ok(VERIFY_WRITE
, argp
, size
))
535 case MEMGETREGIONCOUNT
:
536 if (copy_to_user(argp
, &(mtd
->numeraseregions
), sizeof(int)))
540 case MEMGETREGIONINFO
:
543 struct mtd_erase_region_info
*kr
;
544 struct region_info_user __user
*ur
= argp
;
546 if (get_user(ur_idx
, &(ur
->regionindex
)))
549 kr
= &(mtd
->eraseregions
[ur_idx
]);
551 if (put_user(kr
->offset
, &(ur
->offset
))
552 || put_user(kr
->erasesize
, &(ur
->erasesize
))
553 || put_user(kr
->numblocks
, &(ur
->numblocks
)))
560 info
.type
= mtd
->type
;
561 info
.flags
= mtd
->flags
;
562 info
.size
= mtd
->size
;
563 info
.erasesize
= mtd
->erasesize
;
564 info
.writesize
= mtd
->writesize
;
565 info
.oobsize
= mtd
->oobsize
;
566 /* The below fields are obsolete */
569 if (copy_to_user(argp
, &info
, sizeof(struct mtd_info_user
)))
576 struct erase_info
*erase
;
578 if(!(file
->f_mode
& FMODE_WRITE
))
581 erase
=kzalloc(sizeof(struct erase_info
),GFP_KERNEL
);
585 wait_queue_head_t waitq
;
586 DECLARE_WAITQUEUE(wait
, current
);
588 init_waitqueue_head(&waitq
);
590 if (cmd
== MEMERASE64
) {
591 struct erase_info_user64 einfo64
;
593 if (copy_from_user(&einfo64
, argp
,
594 sizeof(struct erase_info_user64
))) {
598 erase
->addr
= einfo64
.start
;
599 erase
->len
= einfo64
.length
;
601 struct erase_info_user einfo32
;
603 if (copy_from_user(&einfo32
, argp
,
604 sizeof(struct erase_info_user
))) {
608 erase
->addr
= einfo32
.start
;
609 erase
->len
= einfo32
.length
;
612 erase
->callback
= mtdchar_erase_callback
;
613 erase
->priv
= (unsigned long)&waitq
;
616 FIXME: Allow INTERRUPTIBLE. Which means
617 not having the wait_queue head on the stack.
619 If the wq_head is on the stack, and we
620 leave because we got interrupted, then the
621 wq_head is no longer there when the
622 callback routine tries to wake us up.
624 ret
= mtd
->erase(mtd
, erase
);
626 set_current_state(TASK_UNINTERRUPTIBLE
);
627 add_wait_queue(&waitq
, &wait
);
628 if (erase
->state
!= MTD_ERASE_DONE
&&
629 erase
->state
!= MTD_ERASE_FAILED
)
631 remove_wait_queue(&waitq
, &wait
);
632 set_current_state(TASK_RUNNING
);
634 ret
= (erase
->state
== MTD_ERASE_FAILED
)?-EIO
:0;
643 struct mtd_oob_buf buf
;
644 struct mtd_oob_buf __user
*buf_user
= argp
;
646 /* NOTE: writes return length to buf_user->length */
647 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
650 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
, buf
.length
,
651 buf
.ptr
, &buf_user
->length
);
657 struct mtd_oob_buf buf
;
658 struct mtd_oob_buf __user
*buf_user
= argp
;
660 /* NOTE: writes return length to buf_user->start */
661 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
664 ret
= mtd_do_readoob(mtd
, buf
.start
, buf
.length
,
665 buf
.ptr
, &buf_user
->start
);
671 struct mtd_oob_buf64 buf
;
672 struct mtd_oob_buf64 __user
*buf_user
= argp
;
674 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
677 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
, buf
.length
,
678 (void __user
*)(uintptr_t)buf
.usr_ptr
,
685 struct mtd_oob_buf64 buf
;
686 struct mtd_oob_buf64 __user
*buf_user
= argp
;
688 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
691 ret
= mtd_do_readoob(mtd
, buf
.start
, buf
.length
,
692 (void __user
*)(uintptr_t)buf
.usr_ptr
,
699 struct erase_info_user einfo
;
701 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
707 ret
= mtd
->lock(mtd
, einfo
.start
, einfo
.length
);
713 struct erase_info_user einfo
;
715 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
721 ret
= mtd
->unlock(mtd
, einfo
.start
, einfo
.length
);
727 struct erase_info_user einfo
;
729 if (copy_from_user(&einfo
, argp
, sizeof(einfo
)))
735 ret
= mtd
->is_locked(mtd
, einfo
.start
, einfo
.length
);
739 /* Legacy interface */
742 struct nand_oobinfo oi
;
746 if (mtd
->ecclayout
->eccbytes
> ARRAY_SIZE(oi
.eccpos
))
749 oi
.useecc
= MTD_NANDECC_AUTOPLACE
;
750 memcpy(&oi
.eccpos
, mtd
->ecclayout
->eccpos
, sizeof(oi
.eccpos
));
751 memcpy(&oi
.oobfree
, mtd
->ecclayout
->oobfree
,
753 oi
.eccbytes
= mtd
->ecclayout
->eccbytes
;
755 if (copy_to_user(argp
, &oi
, sizeof(struct nand_oobinfo
)))
764 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
766 if (!mtd
->block_isbad
)
769 return mtd
->block_isbad(mtd
, offs
);
777 if (copy_from_user(&offs
, argp
, sizeof(loff_t
)))
779 if (!mtd
->block_markbad
)
782 return mtd
->block_markbad(mtd
, offs
);
786 #ifdef CONFIG_HAVE_MTD_OTP
790 if (copy_from_user(&mode
, argp
, sizeof(int)))
793 mfi
->mode
= MTD_MODE_NORMAL
;
795 ret
= otp_select_filemode(mfi
, mode
);
801 case OTPGETREGIONCOUNT
:
802 case OTPGETREGIONINFO
:
804 struct otp_info
*buf
= kmalloc(4096, GFP_KERNEL
);
809 case MTD_MODE_OTP_FACTORY
:
810 if (mtd
->get_fact_prot_info
)
811 ret
= mtd
->get_fact_prot_info(mtd
, buf
, 4096);
813 case MTD_MODE_OTP_USER
:
814 if (mtd
->get_user_prot_info
)
815 ret
= mtd
->get_user_prot_info(mtd
, buf
, 4096);
821 if (cmd
== OTPGETREGIONCOUNT
) {
822 int nbr
= ret
/ sizeof(struct otp_info
);
823 ret
= copy_to_user(argp
, &nbr
, sizeof(int));
825 ret
= copy_to_user(argp
, buf
, ret
);
835 struct otp_info oinfo
;
837 if (mfi
->mode
!= MTD_MODE_OTP_USER
)
839 if (copy_from_user(&oinfo
, argp
, sizeof(oinfo
)))
841 if (!mtd
->lock_user_prot_reg
)
843 ret
= mtd
->lock_user_prot_reg(mtd
, oinfo
.start
, oinfo
.length
);
848 /* This ioctl is being deprecated - it truncates the ecc layout */
851 struct nand_ecclayout_user
*usrlay
;
856 usrlay
= kmalloc(sizeof(*usrlay
), GFP_KERNEL
);
860 shrink_ecclayout(mtd
->ecclayout
, usrlay
);
862 if (copy_to_user(argp
, usrlay
, sizeof(*usrlay
)))
870 if (copy_to_user(argp
, &mtd
->ecc_stats
,
871 sizeof(struct mtd_ecc_stats
)))
881 case MTD_MODE_OTP_FACTORY
:
882 case MTD_MODE_OTP_USER
:
883 ret
= otp_select_filemode(mfi
, arg
);
887 if (!mtd
->read_oob
|| !mtd
->write_oob
)
891 case MTD_MODE_NORMAL
:
907 static long mtd_unlocked_ioctl(struct file
*file
, u_int cmd
, u_long arg
)
912 ret
= mtd_ioctl(file
, cmd
, arg
);
920 struct mtd_oob_buf32
{
923 compat_caddr_t ptr
; /* unsigned char* */
926 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
927 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
929 static long mtd_compat_ioctl(struct file
*file
, unsigned int cmd
,
932 struct mtd_file_info
*mfi
= file
->private_data
;
933 struct mtd_info
*mtd
= mfi
->mtd
;
934 void __user
*argp
= compat_ptr(arg
);
942 struct mtd_oob_buf32 buf
;
943 struct mtd_oob_buf32 __user
*buf_user
= argp
;
945 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
948 ret
= mtd_do_writeoob(file
, mtd
, buf
.start
,
949 buf
.length
, compat_ptr(buf
.ptr
),
956 struct mtd_oob_buf32 buf
;
957 struct mtd_oob_buf32 __user
*buf_user
= argp
;
959 /* NOTE: writes return length to buf->start */
960 if (copy_from_user(&buf
, argp
, sizeof(buf
)))
963 ret
= mtd_do_readoob(mtd
, buf
.start
,
964 buf
.length
, compat_ptr(buf
.ptr
),
969 ret
= mtd_ioctl(file
, cmd
, (unsigned long)argp
);
977 #endif /* CONFIG_COMPAT */
980 * try to determine where a shared mapping can be made
981 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
985 static unsigned long mtd_get_unmapped_area(struct file
*file
,
991 struct mtd_file_info
*mfi
= file
->private_data
;
992 struct mtd_info
*mtd
= mfi
->mtd
;
994 if (mtd
->get_unmapped_area
) {
995 unsigned long offset
;
998 return (unsigned long) -EINVAL
;
1000 if (len
> mtd
->size
|| pgoff
>= (mtd
->size
>> PAGE_SHIFT
))
1001 return (unsigned long) -EINVAL
;
1003 offset
= pgoff
<< PAGE_SHIFT
;
1004 if (offset
> mtd
->size
- len
)
1005 return (unsigned long) -EINVAL
;
1007 return mtd
->get_unmapped_area(mtd
, len
, offset
, flags
);
1010 /* can't map directly */
1011 return (unsigned long) -ENOSYS
;
1016 * set up a mapping for shared memory segments
1018 static int mtd_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1021 struct mtd_file_info
*mfi
= file
->private_data
;
1022 struct mtd_info
*mtd
= mfi
->mtd
;
1023 struct map_info
*map
= mtd
->priv
;
1024 unsigned long start
;
1028 if (mtd
->type
== MTD_RAM
|| mtd
->type
== MTD_ROM
) {
1029 off
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1031 len
= PAGE_ALIGN((start
& ~PAGE_MASK
) + map
->size
);
1033 if ((vma
->vm_end
- vma
->vm_start
+ off
) > len
)
1037 vma
->vm_pgoff
= off
>> PAGE_SHIFT
;
1038 vma
->vm_flags
|= VM_IO
| VM_RESERVED
;
1040 #ifdef pgprot_noncached
1041 if (file
->f_flags
& O_DSYNC
|| off
>= __pa(high_memory
))
1042 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1044 if (io_remap_pfn_range(vma
, vma
->vm_start
, off
>> PAGE_SHIFT
,
1045 vma
->vm_end
- vma
->vm_start
,
1053 return vma
->vm_flags
& VM_SHARED
? 0 : -ENOSYS
;
1057 static const struct file_operations mtd_fops
= {
1058 .owner
= THIS_MODULE
,
1059 .llseek
= mtd_lseek
,
1062 .unlocked_ioctl
= mtd_unlocked_ioctl
,
1063 #ifdef CONFIG_COMPAT
1064 .compat_ioctl
= mtd_compat_ioctl
,
1067 .release
= mtd_close
,
1070 .get_unmapped_area
= mtd_get_unmapped_area
,
1074 static int mtd_inodefs_get_sb(struct file_system_type
*fs_type
, int flags
,
1075 const char *dev_name
, void *data
,
1076 struct vfsmount
*mnt
)
1078 return get_sb_pseudo(fs_type
, "mtd_inode:", NULL
, MTD_INODE_FS_MAGIC
,
1082 static struct file_system_type mtd_inodefs_type
= {
1083 .name
= "mtd_inodefs",
1084 .get_sb
= mtd_inodefs_get_sb
,
1085 .kill_sb
= kill_anon_super
,
1088 static void mtdchar_notify_add(struct mtd_info
*mtd
)
1092 static void mtdchar_notify_remove(struct mtd_info
*mtd
)
1094 struct inode
*mtd_ino
= ilookup(mtd_inode_mnt
->mnt_sb
, mtd
->index
);
1097 /* Destroy the inode if it exists */
1098 mtd_ino
->i_nlink
= 0;
1103 static struct mtd_notifier mtdchar_notifier
= {
1104 .add
= mtdchar_notify_add
,
1105 .remove
= mtdchar_notify_remove
,
1108 static int __init
init_mtdchar(void)
1112 ret
= __register_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
,
1115 pr_notice("Can't allocate major number %d for "
1116 "Memory Technology Devices.\n", MTD_CHAR_MAJOR
);
1120 ret
= register_filesystem(&mtd_inodefs_type
);
1122 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret
);
1123 goto err_unregister_chdev
;
1126 mtd_inode_mnt
= kern_mount(&mtd_inodefs_type
);
1127 if (IS_ERR(mtd_inode_mnt
)) {
1128 ret
= PTR_ERR(mtd_inode_mnt
);
1129 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret
);
1130 goto err_unregister_filesystem
;
1132 register_mtd_user(&mtdchar_notifier
);
1136 err_unregister_filesystem
:
1137 unregister_filesystem(&mtd_inodefs_type
);
1138 err_unregister_chdev
:
1139 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1143 static void __exit
cleanup_mtdchar(void)
1145 unregister_mtd_user(&mtdchar_notifier
);
1146 mntput(mtd_inode_mnt
);
1147 unregister_filesystem(&mtd_inodefs_type
);
1148 __unregister_chrdev(MTD_CHAR_MAJOR
, 0, 1 << MINORBITS
, "mtd");
1151 module_init(init_mtdchar
);
1152 module_exit(cleanup_mtdchar
);
1154 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);
1156 MODULE_LICENSE("GPL");
1157 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1158 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
1159 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR
);