]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - drivers/mtd/mtdchar.c
mtd: Remove obsolete <mtd/compatmac.h> include
[net-next-2.6.git] / drivers / mtd / mtdchar.c
index 5b081cb84351340dd6164fe448703f93c6dfca47..638827a25b77859ec88eddf99118080f23e431e6 100644 (file)
@@ -1,5 +1,19 @@
 /*
- * Character-device access to raw MTD devices.
+ * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
 #include <linux/smp_lock.h>
 #include <linux/backing-dev.h>
 #include <linux/compat.h>
+#include <linux/mount.h>
 
 #include <linux/mtd/mtd.h>
-#include <linux/mtd/compatmac.h>
+#include <linux/mtd/map.h>
 
 #include <asm/uaccess.h>
 
+#define MTD_INODE_FS_MAGIC 0x11307854
+static struct vfsmount *mtd_inode_mnt __read_mostly;
 
 /*
  * Data structure to hold the pointer to the mtd device as well
@@ -28,6 +45,7 @@
  */
 struct mtd_file_info {
        struct mtd_info *mtd;
+       struct inode *ino;
        enum mtd_file_modes mode;
 };
 
@@ -64,12 +82,10 @@ static int mtd_open(struct inode *inode, struct file *file)
        int ret = 0;
        struct mtd_info *mtd;
        struct mtd_file_info *mfi;
+       struct inode *mtd_ino;
 
        DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
 
-       if (devnum >= MAX_MTD_DEVICES)
-               return -ENODEV;
-
        /* You can't open the RO devices RW */
        if ((file->f_mode & FMODE_WRITE) && (minor & 1))
                return -EACCES;
@@ -88,11 +104,23 @@ static int mtd_open(struct inode *inode, struct file *file)
                goto out;
        }
 
-       if (mtd->backing_dev_info)
-               file->f_mapping->backing_dev_info = mtd->backing_dev_info;
+       mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
+       if (!mtd_ino) {
+               put_mtd_device(mtd);
+               ret = -ENOMEM;
+               goto out;
+       }
+       if (mtd_ino->i_state & I_NEW) {
+               mtd_ino->i_private = mtd;
+               mtd_ino->i_mode = S_IFCHR;
+               mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
+               unlock_new_inode(mtd_ino);
+       }
+       file->f_mapping = mtd_ino->i_mapping;
 
        /* You can't open it RW if it's not a writeable device */
        if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
+               iput(mtd_ino);
                put_mtd_device(mtd);
                ret = -EACCES;
                goto out;
@@ -100,10 +128,12 @@ static int mtd_open(struct inode *inode, struct file *file)
 
        mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
        if (!mfi) {
+               iput(mtd_ino);
                put_mtd_device(mtd);
                ret = -ENOMEM;
                goto out;
        }
+       mfi->ino = mtd_ino;
        mfi->mtd = mtd;
        file->private_data = mfi;
 
@@ -125,6 +155,8 @@ static int mtd_close(struct inode *inode, struct file *file)
        if ((file->f_mode & FMODE_WRITE) && mtd->sync)
                mtd->sync(mtd);
 
+       iput(mfi->ino);
+
        put_mtd_device(mtd);
        file->private_data = NULL;
        kfree(mfi);
@@ -373,7 +405,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
        if (!mtd->write_oob)
                ret = -EOPNOTSUPP;
        else
-               ret = access_ok(VERIFY_READ, ptr, length) ? 0 : EFAULT;
+               ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
 
        if (ret)
                return ret;
@@ -386,14 +418,9 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
        if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
                return -EINVAL;
 
-       ops.oobbuf = kmalloc(length, GFP_KERNEL);
-       if (!ops.oobbuf)
-               return -ENOMEM;
-
-       if (copy_from_user(ops.oobbuf, ptr, length)) {
-               kfree(ops.oobbuf);
-               return -EFAULT;
-       }
+       ops.oobbuf = memdup_user(ptr, length);
+       if (IS_ERR(ops.oobbuf))
+               return PTR_ERR(ops.oobbuf);
 
        start &= ~((uint64_t)mtd->oobsize - 1);
        ret = mtd->write_oob(mtd, start, &ops);
@@ -482,7 +509,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
        {
                uint32_t ur_idx;
                struct mtd_erase_region_info *kr;
-               struct region_info_user *ur = (struct region_info_user *) argp;
+               struct region_info_user __user *ur = argp;
 
                if (get_user(ur_idx, &(ur->regionindex)))
                        return -EFAULT;
@@ -663,6 +690,20 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
                break;
        }
 
+       case MEMISLOCKED:
+       {
+               struct erase_info_user einfo;
+
+               if (copy_from_user(&einfo, argp, sizeof(einfo)))
+                       return -EFAULT;
+
+               if (!mtd->is_locked)
+                       ret = -EOPNOTSUPP;
+               else
+                       ret = mtd->is_locked(mtd, einfo.start, einfo.length);
+               break;
+       }
+
        /* Legacy interface */
        case MEMGETOOBSEL:
        {
@@ -928,9 +969,34 @@ static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
 #ifdef CONFIG_MMU
        struct mtd_file_info *mfi = file->private_data;
        struct mtd_info *mtd = mfi->mtd;
+       struct map_info *map = mtd->priv;
+       unsigned long start;
+       unsigned long off;
+       u32 len;
+
+       if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
+               off = vma->vm_pgoff << PAGE_SHIFT;
+               start = map->phys;
+               len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
+               start &= PAGE_MASK;
+               if ((vma->vm_end - vma->vm_start + off) > len)
+                       return -EINVAL;
+
+               off += start;
+               vma->vm_pgoff = off >> PAGE_SHIFT;
+               vma->vm_flags |= VM_IO | VM_RESERVED;
+
+#ifdef pgprot_noncached
+               if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
+                       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+#endif
+               if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
+                                      vma->vm_end - vma->vm_start,
+                                      vma->vm_page_prot))
+                       return -EAGAIN;
 
-       if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
                return 0;
+       }
        return -ENOSYS;
 #else
        return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
@@ -954,22 +1020,81 @@ static const struct file_operations mtd_fops = {
 #endif
 };
 
+static int mtd_inodefs_get_sb(struct file_system_type *fs_type, int flags,
+                               const char *dev_name, void *data,
+                               struct vfsmount *mnt)
+{
+        return get_sb_pseudo(fs_type, "mtd_inode:", NULL, MTD_INODE_FS_MAGIC,
+                             mnt);
+}
+
+static struct file_system_type mtd_inodefs_type = {
+       .name = "mtd_inodefs",
+       .get_sb = mtd_inodefs_get_sb,
+       .kill_sb = kill_anon_super,
+};
+
+static void mtdchar_notify_add(struct mtd_info *mtd)
+{
+}
+
+static void mtdchar_notify_remove(struct mtd_info *mtd)
+{
+       struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
+
+       if (mtd_ino) {
+               /* Destroy the inode if it exists */
+               mtd_ino->i_nlink = 0;
+               iput(mtd_ino);
+       }
+}
+
+static struct mtd_notifier mtdchar_notifier = {
+       .add = mtdchar_notify_add,
+       .remove = mtdchar_notify_remove,
+};
+
 static int __init init_mtdchar(void)
 {
-       int status;
+       int ret;
 
-       status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops);
-       if (status < 0) {
-               printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
-                      MTD_CHAR_MAJOR);
+       ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
+                                  "mtd", &mtd_fops);
+       if (ret < 0) {
+               pr_notice("Can't allocate major number %d for "
+                               "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
+               return ret;
        }
 
-       return status;
+       ret = register_filesystem(&mtd_inodefs_type);
+       if (ret) {
+               pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
+               goto err_unregister_chdev;
+       }
+
+       mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
+       if (IS_ERR(mtd_inode_mnt)) {
+               ret = PTR_ERR(mtd_inode_mnt);
+               pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
+               goto err_unregister_filesystem;
+       }
+       register_mtd_user(&mtdchar_notifier);
+
+       return ret;
+
+err_unregister_filesystem:
+       unregister_filesystem(&mtd_inodefs_type);
+err_unregister_chdev:
+       __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
+       return ret;
 }
 
 static void __exit cleanup_mtdchar(void)
 {
-       unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+       unregister_mtd_user(&mtdchar_notifier);
+       mntput(mtd_inode_mnt);
+       unregister_filesystem(&mtd_inodefs_type);
+       __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
 }
 
 module_init(init_mtdchar);