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/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
36 #include <linux/mtd/map.h>
38 #include <asm/uaccess.h>
40 #define MTD_INODE_FS_MAGIC 0x11307854
41 static DEFINE_MUTEX(mtd_mutex);
42 static struct vfsmount *mtd_inode_mnt __read_mostly;
45 * Data structure to hold the pointer to the mtd device as well
46 * as mode information ofr various use cases.
48 struct mtd_file_info {
51 enum mtd_file_modes mode;
54 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
56 struct mtd_file_info *mfi = file->private_data;
57 struct mtd_info *mtd = mfi->mtd;
63 offset += file->f_pos;
72 if (offset >= 0 && offset <= mtd->size)
73 return file->f_pos = offset;
80 static int mtd_open(struct inode *inode, struct file *file)
82 int minor = iminor(inode);
83 int devnum = minor >> 1;
86 struct mtd_file_info *mfi;
87 struct inode *mtd_ino;
89 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
91 /* You can't open the RO devices RW */
92 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
95 mutex_lock(&mtd_mutex);
96 mtd = get_mtd_device(NULL, devnum);
103 if (mtd->type == MTD_ABSENT) {
109 mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
115 if (mtd_ino->i_state & I_NEW) {
116 mtd_ino->i_private = mtd;
117 mtd_ino->i_mode = S_IFCHR;
118 mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
119 unlock_new_inode(mtd_ino);
121 file->f_mapping = mtd_ino->i_mapping;
123 /* You can't open it RW if it's not a writeable device */
124 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
131 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
140 file->private_data = mfi;
143 mutex_unlock(&mtd_mutex);
147 /*====================================================================*/
149 static int mtd_close(struct inode *inode, struct file *file)
151 struct mtd_file_info *mfi = file->private_data;
152 struct mtd_info *mtd = mfi->mtd;
154 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
156 /* Only sync if opened RW */
157 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
163 file->private_data = NULL;
169 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
170 userspace buffer down and use it directly with readv/writev.
172 #define MAX_KMALLOC_SIZE 0x20000
174 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
176 struct mtd_file_info *mfi = file->private_data;
177 struct mtd_info *mtd = mfi->mtd;
179 size_t total_retlen=0;
184 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
186 if (*ppos + count > mtd->size)
187 count = mtd->size - *ppos;
192 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
193 and pass them directly to the MTD functions */
195 if (count > MAX_KMALLOC_SIZE)
196 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
198 kbuf=kmalloc(count, GFP_KERNEL);
205 if (count > MAX_KMALLOC_SIZE)
206 len = MAX_KMALLOC_SIZE;
211 case MTD_MODE_OTP_FACTORY:
212 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
214 case MTD_MODE_OTP_USER:
215 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
219 struct mtd_oob_ops ops;
221 ops.mode = MTD_OOB_RAW;
226 ret = mtd->read_oob(mtd, *ppos, &ops);
231 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
233 /* Nand returns -EBADMSG on ecc errors, but it returns
234 * the data. For our userspace tools it is important
235 * to dump areas with ecc errors !
236 * For kernel internal usage it also might return -EUCLEAN
237 * to signal the caller that a bitflip has occured and has
238 * been corrected by the ECC algorithm.
239 * Userspace software which accesses NAND this way
240 * must be aware of the fact that it deals with NAND
242 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
244 if (copy_to_user(buf, kbuf, retlen)) {
249 total_retlen += retlen;
267 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
269 struct mtd_file_info *mfi = file->private_data;
270 struct mtd_info *mtd = mfi->mtd;
273 size_t total_retlen=0;
277 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
279 if (*ppos == mtd->size)
282 if (*ppos + count > mtd->size)
283 count = mtd->size - *ppos;
288 if (count > MAX_KMALLOC_SIZE)
289 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
291 kbuf=kmalloc(count, GFP_KERNEL);
298 if (count > MAX_KMALLOC_SIZE)
299 len = MAX_KMALLOC_SIZE;
303 if (copy_from_user(kbuf, buf, len)) {
309 case MTD_MODE_OTP_FACTORY:
312 case MTD_MODE_OTP_USER:
313 if (!mtd->write_user_prot_reg) {
317 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
322 struct mtd_oob_ops ops;
324 ops.mode = MTD_OOB_RAW;
329 ret = mtd->write_oob(mtd, *ppos, &ops);
335 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
339 total_retlen += retlen;
353 /*======================================================================
355 IOCTL calls for getting device parameters.
357 ======================================================================*/
358 static void mtdchar_erase_callback (struct erase_info *instr)
360 wake_up((wait_queue_head_t *)instr->priv);
363 #ifdef CONFIG_HAVE_MTD_OTP
364 static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
366 struct mtd_info *mtd = mfi->mtd;
370 case MTD_OTP_FACTORY:
371 if (!mtd->read_fact_prot_reg)
374 mfi->mode = MTD_MODE_OTP_FACTORY;
377 if (!mtd->read_fact_prot_reg)
380 mfi->mode = MTD_MODE_OTP_USER;
390 # define otp_select_filemode(f,m) -EOPNOTSUPP
393 static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
394 uint64_t start, uint32_t length, void __user *ptr,
395 uint32_t __user *retp)
397 struct mtd_oob_ops ops;
401 if (!(file->f_mode & FMODE_WRITE))
410 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
416 ops.ooboffs = start & (mtd->oobsize - 1);
418 ops.mode = MTD_OOB_PLACE;
420 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
423 ops.oobbuf = memdup_user(ptr, length);
424 if (IS_ERR(ops.oobbuf))
425 return PTR_ERR(ops.oobbuf);
427 start &= ~((uint64_t)mtd->oobsize - 1);
428 ret = mtd->write_oob(mtd, start, &ops);
430 if (ops.oobretlen > 0xFFFFFFFFU)
432 retlen = ops.oobretlen;
433 if (copy_to_user(retp, &retlen, sizeof(length)))
440 static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
441 uint32_t length, void __user *ptr, uint32_t __user *retp)
443 struct mtd_oob_ops ops;
452 ret = access_ok(VERIFY_WRITE, ptr,
453 length) ? 0 : -EFAULT;
458 ops.ooboffs = start & (mtd->oobsize - 1);
460 ops.mode = MTD_OOB_PLACE;
462 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
465 ops.oobbuf = kmalloc(length, GFP_KERNEL);
469 start &= ~((uint64_t)mtd->oobsize - 1);
470 ret = mtd->read_oob(mtd, start, &ops);
472 if (put_user(ops.oobretlen, retp))
474 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
483 * Copies (and truncates, if necessary) data from the larger struct,
484 * nand_ecclayout, to the smaller, deprecated layout struct,
485 * nand_ecclayout_user. This is necessary only to suppport the deprecated
486 * API ioctl ECCGETLAYOUT while allowing all new functionality to use
487 * nand_ecclayout flexibly (i.e. the struct may change size in new
488 * releases without requiring major rewrites).
490 static int shrink_ecclayout(const struct nand_ecclayout *from,
491 struct nand_ecclayout_user *to)
498 memset(to, 0, sizeof(*to));
500 to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
501 for (i = 0; i < to->eccbytes; i++)
502 to->eccpos[i] = from->eccpos[i];
504 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
505 if (from->oobfree[i].length == 0 &&
506 from->oobfree[i].offset == 0)
508 to->oobavail += from->oobfree[i].length;
509 to->oobfree[i] = from->oobfree[i];
515 #ifdef CONFIG_MTD_PARTITIONS
516 static int mtd_blkpg_ioctl(struct mtd_info *mtd,
517 struct blkpg_ioctl_arg __user *arg)
519 struct blkpg_ioctl_arg a;
520 struct blkpg_partition p;
522 if (!capable(CAP_SYS_ADMIN))
525 /* Only master mtd device must be used to control partitions */
526 if (!mtd_is_master(mtd))
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 return mtd_add_partition(mtd, p.devname, p.start, p.length);
540 case BLKPG_DEL_PARTITION:
545 return mtd_del_partition(mtd, p.pno);
554 static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
556 struct mtd_file_info *mfi = file->private_data;
557 struct mtd_info *mtd = mfi->mtd;
558 void __user *argp = (void __user *)arg;
561 struct mtd_info_user info;
563 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
565 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
567 if (!access_ok(VERIFY_READ, argp, size))
571 if (!access_ok(VERIFY_WRITE, argp, size))
576 case MEMGETREGIONCOUNT:
577 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
581 case MEMGETREGIONINFO:
584 struct mtd_erase_region_info *kr;
585 struct region_info_user __user *ur = argp;
587 if (get_user(ur_idx, &(ur->regionindex)))
590 if (ur_idx >= mtd->numeraseregions)
593 kr = &(mtd->eraseregions[ur_idx]);
595 if (put_user(kr->offset, &(ur->offset))
596 || put_user(kr->erasesize, &(ur->erasesize))
597 || put_user(kr->numblocks, &(ur->numblocks)))
604 info.type = mtd->type;
605 info.flags = mtd->flags;
606 info.size = mtd->size;
607 info.erasesize = mtd->erasesize;
608 info.writesize = mtd->writesize;
609 info.oobsize = mtd->oobsize;
610 /* The below fields are obsolete */
613 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
620 struct erase_info *erase;
622 if(!(file->f_mode & FMODE_WRITE))
625 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
629 wait_queue_head_t waitq;
630 DECLARE_WAITQUEUE(wait, current);
632 init_waitqueue_head(&waitq);
634 if (cmd == MEMERASE64) {
635 struct erase_info_user64 einfo64;
637 if (copy_from_user(&einfo64, argp,
638 sizeof(struct erase_info_user64))) {
642 erase->addr = einfo64.start;
643 erase->len = einfo64.length;
645 struct erase_info_user einfo32;
647 if (copy_from_user(&einfo32, argp,
648 sizeof(struct erase_info_user))) {
652 erase->addr = einfo32.start;
653 erase->len = einfo32.length;
656 erase->callback = mtdchar_erase_callback;
657 erase->priv = (unsigned long)&waitq;
660 FIXME: Allow INTERRUPTIBLE. Which means
661 not having the wait_queue head on the stack.
663 If the wq_head is on the stack, and we
664 leave because we got interrupted, then the
665 wq_head is no longer there when the
666 callback routine tries to wake us up.
668 ret = mtd->erase(mtd, erase);
670 set_current_state(TASK_UNINTERRUPTIBLE);
671 add_wait_queue(&waitq, &wait);
672 if (erase->state != MTD_ERASE_DONE &&
673 erase->state != MTD_ERASE_FAILED)
675 remove_wait_queue(&waitq, &wait);
676 set_current_state(TASK_RUNNING);
678 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
687 struct mtd_oob_buf buf;
688 struct mtd_oob_buf __user *buf_user = argp;
690 /* NOTE: writes return length to buf_user->length */
691 if (copy_from_user(&buf, argp, sizeof(buf)))
694 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
695 buf.ptr, &buf_user->length);
701 struct mtd_oob_buf buf;
702 struct mtd_oob_buf __user *buf_user = argp;
704 /* NOTE: writes return length to buf_user->start */
705 if (copy_from_user(&buf, argp, sizeof(buf)))
708 ret = mtd_do_readoob(mtd, buf.start, buf.length,
709 buf.ptr, &buf_user->start);
715 struct mtd_oob_buf64 buf;
716 struct mtd_oob_buf64 __user *buf_user = argp;
718 if (copy_from_user(&buf, argp, sizeof(buf)))
721 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
722 (void __user *)(uintptr_t)buf.usr_ptr,
729 struct mtd_oob_buf64 buf;
730 struct mtd_oob_buf64 __user *buf_user = argp;
732 if (copy_from_user(&buf, argp, sizeof(buf)))
735 ret = mtd_do_readoob(mtd, buf.start, buf.length,
736 (void __user *)(uintptr_t)buf.usr_ptr,
743 struct erase_info_user einfo;
745 if (copy_from_user(&einfo, argp, sizeof(einfo)))
751 ret = mtd->lock(mtd, einfo.start, einfo.length);
757 struct erase_info_user einfo;
759 if (copy_from_user(&einfo, argp, sizeof(einfo)))
765 ret = mtd->unlock(mtd, einfo.start, einfo.length);
771 struct erase_info_user einfo;
773 if (copy_from_user(&einfo, argp, sizeof(einfo)))
779 ret = mtd->is_locked(mtd, einfo.start, einfo.length);
783 /* Legacy interface */
786 struct nand_oobinfo oi;
790 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
793 oi.useecc = MTD_NANDECC_AUTOPLACE;
794 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
795 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
797 oi.eccbytes = mtd->ecclayout->eccbytes;
799 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
808 if (copy_from_user(&offs, argp, sizeof(loff_t)))
810 if (!mtd->block_isbad)
813 return mtd->block_isbad(mtd, offs);
821 if (copy_from_user(&offs, argp, sizeof(loff_t)))
823 if (!mtd->block_markbad)
826 return mtd->block_markbad(mtd, offs);
830 #ifdef CONFIG_HAVE_MTD_OTP
834 if (copy_from_user(&mode, argp, sizeof(int)))
837 mfi->mode = MTD_MODE_NORMAL;
839 ret = otp_select_filemode(mfi, mode);
845 case OTPGETREGIONCOUNT:
846 case OTPGETREGIONINFO:
848 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
853 case MTD_MODE_OTP_FACTORY:
854 if (mtd->get_fact_prot_info)
855 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
857 case MTD_MODE_OTP_USER:
858 if (mtd->get_user_prot_info)
859 ret = mtd->get_user_prot_info(mtd, buf, 4096);
865 if (cmd == OTPGETREGIONCOUNT) {
866 int nbr = ret / sizeof(struct otp_info);
867 ret = copy_to_user(argp, &nbr, sizeof(int));
869 ret = copy_to_user(argp, buf, ret);
879 struct otp_info oinfo;
881 if (mfi->mode != MTD_MODE_OTP_USER)
883 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
885 if (!mtd->lock_user_prot_reg)
887 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
892 /* This ioctl is being deprecated - it truncates the ecc layout */
895 struct nand_ecclayout_user *usrlay;
900 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
904 shrink_ecclayout(mtd->ecclayout, usrlay);
906 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
914 if (copy_to_user(argp, &mtd->ecc_stats,
915 sizeof(struct mtd_ecc_stats)))
925 case MTD_MODE_OTP_FACTORY:
926 case MTD_MODE_OTP_USER:
927 ret = otp_select_filemode(mfi, arg);
931 if (!mtd->read_oob || !mtd->write_oob)
935 case MTD_MODE_NORMAL:
944 #ifdef CONFIG_MTD_PARTITIONS
947 ret = mtd_blkpg_ioctl(mtd,
948 (struct blkpg_ioctl_arg __user *)arg);
954 /* No reread partition feature. Just return ok */
967 static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
971 mutex_lock(&mtd_mutex);
972 ret = mtd_ioctl(file, cmd, arg);
973 mutex_unlock(&mtd_mutex);
980 struct mtd_oob_buf32 {
983 compat_caddr_t ptr; /* unsigned char* */
986 #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
987 #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
989 static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
992 struct mtd_file_info *mfi = file->private_data;
993 struct mtd_info *mtd = mfi->mtd;
994 void __user *argp = compat_ptr(arg);
997 mutex_lock(&mtd_mutex);
1002 struct mtd_oob_buf32 buf;
1003 struct mtd_oob_buf32 __user *buf_user = argp;
1005 if (copy_from_user(&buf, argp, sizeof(buf)))
1008 ret = mtd_do_writeoob(file, mtd, buf.start,
1009 buf.length, compat_ptr(buf.ptr),
1016 struct mtd_oob_buf32 buf;
1017 struct mtd_oob_buf32 __user *buf_user = argp;
1019 /* NOTE: writes return length to buf->start */
1020 if (copy_from_user(&buf, argp, sizeof(buf)))
1023 ret = mtd_do_readoob(mtd, buf.start,
1024 buf.length, compat_ptr(buf.ptr),
1029 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
1032 mutex_unlock(&mtd_mutex);
1037 #endif /* CONFIG_COMPAT */
1040 * try to determine where a shared mapping can be made
1041 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1045 static unsigned long mtd_get_unmapped_area(struct file *file,
1048 unsigned long pgoff,
1049 unsigned long flags)
1051 struct mtd_file_info *mfi = file->private_data;
1052 struct mtd_info *mtd = mfi->mtd;
1054 if (mtd->get_unmapped_area) {
1055 unsigned long offset;
1058 return (unsigned long) -EINVAL;
1060 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1061 return (unsigned long) -EINVAL;
1063 offset = pgoff << PAGE_SHIFT;
1064 if (offset > mtd->size - len)
1065 return (unsigned long) -EINVAL;
1067 return mtd->get_unmapped_area(mtd, len, offset, flags);
1070 /* can't map directly */
1071 return (unsigned long) -ENOSYS;
1076 * set up a mapping for shared memory segments
1078 static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
1081 struct mtd_file_info *mfi = file->private_data;
1082 struct mtd_info *mtd = mfi->mtd;
1083 struct map_info *map = mtd->priv;
1084 unsigned long start;
1088 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
1089 off = vma->vm_pgoff << PAGE_SHIFT;
1091 len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
1093 if ((vma->vm_end - vma->vm_start + off) > len)
1097 vma->vm_pgoff = off >> PAGE_SHIFT;
1098 vma->vm_flags |= VM_IO | VM_RESERVED;
1100 #ifdef pgprot_noncached
1101 if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
1102 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1104 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1105 vma->vm_end - vma->vm_start,
1113 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
1117 static const struct file_operations mtd_fops = {
1118 .owner = THIS_MODULE,
1119 .llseek = mtd_lseek,
1122 .unlocked_ioctl = mtd_unlocked_ioctl,
1123 #ifdef CONFIG_COMPAT
1124 .compat_ioctl = mtd_compat_ioctl,
1127 .release = mtd_close,
1130 .get_unmapped_area = mtd_get_unmapped_area,
1134 static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
1135 int flags, const char *dev_name, void *data)
1137 return mount_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC);
1140 static struct file_system_type mtd_inodefs_type = {
1141 .name = "mtd_inodefs",
1142 .mount = mtd_inodefs_mount,
1143 .kill_sb = kill_anon_super,
1146 static void mtdchar_notify_add(struct mtd_info *mtd)
1150 static void mtdchar_notify_remove(struct mtd_info *mtd)
1152 struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
1155 /* Destroy the inode if it exists */
1156 mtd_ino->i_nlink = 0;
1161 static struct mtd_notifier mtdchar_notifier = {
1162 .add = mtdchar_notify_add,
1163 .remove = mtdchar_notify_remove,
1166 static int __init init_mtdchar(void)
1170 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
1173 pr_notice("Can't allocate major number %d for "
1174 "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
1178 ret = register_filesystem(&mtd_inodefs_type);
1180 pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
1181 goto err_unregister_chdev;
1184 mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
1185 if (IS_ERR(mtd_inode_mnt)) {
1186 ret = PTR_ERR(mtd_inode_mnt);
1187 pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
1188 goto err_unregister_filesystem;
1190 register_mtd_user(&mtdchar_notifier);
1194 err_unregister_filesystem:
1195 unregister_filesystem(&mtd_inodefs_type);
1196 err_unregister_chdev:
1197 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1201 static void __exit cleanup_mtdchar(void)
1203 unregister_mtd_user(&mtdchar_notifier);
1204 mntput_long(mtd_inode_mnt);
1205 unregister_filesystem(&mtd_inodefs_type);
1206 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
1209 module_init(init_mtdchar);
1210 module_exit(cleanup_mtdchar);
1212 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
1214 MODULE_LICENSE("GPL");
1215 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1216 MODULE_DESCRIPTION("Direct character-device access to MTD devices");
1217 MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);