fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / mtd / mtdchar.c
index 801bcc8..3013d08 100644 (file)
@@ -1,58 +1,84 @@
 /*
- * $Id: mtdchar.c,v 1.54 2003/05/21 10:50:43 dwmw2 Exp $
+ * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
  *
  * Character-device access to raw MTD devices.
  *
  */
 
-#include <linux/config.h>
+#include <linux/device.h>
+#include <linux/fs.h>
+#include <linux/err.h>
+#include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/mtd/mtd.h>
 #include <linux/slab.h>
-#include <linux/init.h>
-#include <linux/fs.h>
+#include <linux/sched.h>
+
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/compatmac.h>
+
 #include <asm/uaccess.h>
 
-#ifdef CONFIG_DEVFS_FS
-#include <linux/devfs_fs_kernel.h>
-static void mtd_notify_add(struct mtd_info* mtd);
-static void mtd_notify_remove(struct mtd_info* mtd);
+static struct class *mtd_class;
+
+static void mtd_notify_add(struct mtd_info* mtd)
+{
+       if (!mtd)
+               return;
+
+       class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
+                           NULL, "mtd%d", mtd->index);
+
+       class_device_create(mtd_class, NULL,
+                           MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
+                           NULL, "mtd%dro", mtd->index);
+}
+
+static void mtd_notify_remove(struct mtd_info* mtd)
+{
+       if (!mtd)
+               return;
+
+       class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
+       class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
+}
 
 static struct mtd_notifier notifier = {
        .add    = mtd_notify_add,
        .remove = mtd_notify_remove,
 };
 
-#endif
+/*
+ * Data structure to hold the pointer to the mtd device as well
+ * as mode information ofr various use cases.
+ */
+struct mtd_file_info {
+       struct mtd_info *mtd;
+       enum mtd_file_modes mode;
+};
 
 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
 {
-       struct mtd_info *mtd=(struct mtd_info *)file->private_data;
+       struct mtd_file_info *mfi = file->private_data;
+       struct mtd_info *mtd = mfi->mtd;
 
        switch (orig) {
-       case 0:
-               /* SEEK_SET */
-               file->f_pos = offset;
+       case SEEK_SET:
                break;
-       case 1:
-               /* SEEK_CUR */
-               file->f_pos += offset;
+       case SEEK_CUR:
+               offset += file->f_pos;
                break;
-       case 2:
-               /* SEEK_END */
-               file->f_pos =mtd->size + offset;
+       case SEEK_END:
+               offset += mtd->size;
                break;
        default:
                return -EINVAL;
        }
 
-       if (file->f_pos < 0)
-               file->f_pos = 0;
-       else if (file->f_pos >= mtd->size)
-               file->f_pos = mtd->size - 1;
+       if (offset >= 0 && offset <= mtd->size)
+               return file->f_pos = offset;
 
-       return file->f_pos;
+       return -EINVAL;
 }
 
 
@@ -62,6 +88,7 @@ static int mtd_open(struct inode *inode, struct file *file)
        int minor = iminor(inode);
        int devnum = minor >> 1;
        struct mtd_info *mtd;
+       struct mtd_file_info *mfi;
 
        DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
 
@@ -73,23 +100,29 @@ static int mtd_open(struct inode *inode, struct file *file)
                return -EACCES;
 
        mtd = get_mtd_device(NULL, devnum);
-       
-       if (!mtd)
-               return -ENODEV;
-       
+
+       if (IS_ERR(mtd))
+               return PTR_ERR(mtd);
+
        if (MTD_ABSENT == mtd->type) {
                put_mtd_device(mtd);
                return -ENODEV;
        }
 
-       file->private_data = mtd;
-               
        /* You can't open it RW if it's not a writeable device */
        if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
                put_mtd_device(mtd);
                return -EACCES;
        }
-               
+
+       mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
+       if (!mfi) {
+               put_mtd_device(mtd);
+               return -ENOMEM;
+       }
+       mfi->mtd = mtd;
+       file->private_data = mfi;
+
        return 0;
 } /* mtd_open */
 
@@ -97,16 +130,17 @@ static int mtd_open(struct inode *inode, struct file *file)
 
 static int mtd_close(struct inode *inode, struct file *file)
 {
-       struct mtd_info *mtd;
+       struct mtd_file_info *mfi = file->private_data;
+       struct mtd_info *mtd = mfi->mtd;
 
        DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
 
-       mtd = (struct mtd_info *)file->private_data;
-       
        if (mtd->sync)
                mtd->sync(mtd);
-       
+
        put_mtd_device(mtd);
+       file->private_data = NULL;
+       kfree(mfi);
 
        return 0;
 } /* mtd_close */
@@ -116,15 +150,16 @@ static int mtd_close(struct inode *inode, struct file *file)
 */
 #define MAX_KMALLOC_SIZE 0x20000
 
-static ssize_t mtd_read(struct file *file, char *buf, size_t count,loff_t *ppos)
+static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
 {
-       struct mtd_info *mtd = (struct mtd_info *)file->private_data;
+       struct mtd_file_info *mfi = file->private_data;
+       struct mtd_info *mtd = mfi->mtd;
        size_t retlen=0;
        size_t total_retlen=0;
        int ret=0;
        int len;
        char *kbuf;
-       
+
        DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
 
        if (*ppos + count > mtd->size)
@@ -132,24 +167,61 @@ static ssize_t mtd_read(struct file *file, char *buf, size_t count,loff_t *ppos)
 
        if (!count)
                return 0;
-       
+
        /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
           and pass them directly to the MTD functions */
+
+       if (count > MAX_KMALLOC_SIZE)
+               kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
+       else
+               kbuf=kmalloc(count, GFP_KERNEL);
+
+       if (!kbuf)
+               return -ENOMEM;
+
        while (count) {
-               if (count > MAX_KMALLOC_SIZE) 
+
+               if (count > MAX_KMALLOC_SIZE)
                        len = MAX_KMALLOC_SIZE;
                else
                        len = count;
 
-               kbuf=kmalloc(len,GFP_KERNEL);
-               if (!kbuf)
-                       return -ENOMEM;
-               
-               ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
-               if (!ret) {
+               switch (mfi->mode) {
+               case MTD_MODE_OTP_FACTORY:
+                       ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+                       break;
+               case MTD_MODE_OTP_USER:
+                       ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+                       break;
+               case MTD_MODE_RAW:
+               {
+                       struct mtd_oob_ops ops;
+
+                       ops.mode = MTD_OOB_RAW;
+                       ops.datbuf = kbuf;
+                       ops.oobbuf = NULL;
+                       ops.len = len;
+
+                       ret = mtd->read_oob(mtd, *ppos, &ops);
+                       retlen = ops.retlen;
+                       break;
+               }
+               default:
+                       ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
+               }
+               /* Nand returns -EBADMSG on ecc errors, but it returns
+                * the data. For our userspace tools it is important
+                * to dump areas with ecc errors !
+                * For kernel internal usage it also might return -EUCLEAN
+                * to signal the caller that a bitflip has occured and has
+                * been corrected by the ECC algorithm.
+                * Userspace software which accesses NAND this way
+                * must be aware of the fact that it deals with NAND
+                */
+               if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
                        *ppos += retlen;
                        if (copy_to_user(buf, kbuf, retlen)) {
-                               kfree(kbuf);
+                               kfree(kbuf);
                                return -EFAULT;
                        }
                        else
@@ -157,21 +229,24 @@ static ssize_t mtd_read(struct file *file, char *buf, size_t count,loff_t *ppos)
 
                        count -= retlen;
                        buf += retlen;
+                       if (retlen == 0)
+                               count = 0;
                }
                else {
                        kfree(kbuf);
                        return ret;
                }
-               
-               kfree(kbuf);
+
        }
-       
+
+       kfree(kbuf);
        return total_retlen;
 } /* mtd_read */
 
-static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t *ppos)
+static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
 {
-       struct mtd_info *mtd = (struct mtd_info *)file->private_data;
+       struct mtd_file_info *mfi = file->private_data;
+       struct mtd_info *mtd = mfi->mtd;
        char *kbuf;
        size_t retlen;
        size_t total_retlen=0;
@@ -179,34 +254,65 @@ static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t
        int len;
 
        DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
-       
+
        if (*ppos == mtd->size)
                return -ENOSPC;
-       
+
        if (*ppos + count > mtd->size)
                count = mtd->size - *ppos;
 
        if (!count)
                return 0;
 
+       if (count > MAX_KMALLOC_SIZE)
+               kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
+       else
+               kbuf=kmalloc(count, GFP_KERNEL);
+
+       if (!kbuf)
+               return -ENOMEM;
+
        while (count) {
-               if (count > MAX_KMALLOC_SIZE) 
+
+               if (count > MAX_KMALLOC_SIZE)
                        len = MAX_KMALLOC_SIZE;
                else
                        len = count;
 
-               kbuf=kmalloc(len,GFP_KERNEL);
-               if (!kbuf) {
-                       printk("kmalloc is null\n");
-                       return -ENOMEM;
-               }
-
                if (copy_from_user(kbuf, buf, len)) {
                        kfree(kbuf);
                        return -EFAULT;
                }
-               
-               ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
+
+               switch (mfi->mode) {
+               case MTD_MODE_OTP_FACTORY:
+                       ret = -EROFS;
+                       break;
+               case MTD_MODE_OTP_USER:
+                       if (!mtd->write_user_prot_reg) {
+                               ret = -EOPNOTSUPP;
+                               break;
+                       }
+                       ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
+                       break;
+
+               case MTD_MODE_RAW:
+               {
+                       struct mtd_oob_ops ops;
+
+                       ops.mode = MTD_OOB_RAW;
+                       ops.datbuf = kbuf;
+                       ops.oobbuf = NULL;
+                       ops.len = len;
+
+                       ret = mtd->write_oob(mtd, *ppos, &ops);
+                       retlen = ops.retlen;
+                       break;
+               }
+
+               default:
+                       ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
+               }
                if (!ret) {
                        *ppos += retlen;
                        total_retlen += retlen;
@@ -217,10 +323,9 @@ static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t
                        kfree(kbuf);
                        return ret;
                }
-               
-               kfree(kbuf);
        }
 
+       kfree(kbuf);
        return total_retlen;
 } /* mtd_write */
 
@@ -229,33 +334,66 @@ static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t
     IOCTL calls for getting device parameters.
 
 ======================================================================*/
-static void mtd_erase_callback (struct erase_info *instr)
+static void mtdchar_erase_callback (struct erase_info *instr)
 {
        wake_up((wait_queue_head_t *)instr->priv);
 }
 
+#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
+static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
+{
+       struct mtd_info *mtd = mfi->mtd;
+       int ret = 0;
+
+       switch (mode) {
+       case MTD_OTP_FACTORY:
+               if (!mtd->read_fact_prot_reg)
+                       ret = -EOPNOTSUPP;
+               else
+                       mfi->mode = MTD_MODE_OTP_FACTORY;
+               break;
+       case MTD_OTP_USER:
+               if (!mtd->read_fact_prot_reg)
+                       ret = -EOPNOTSUPP;
+               else
+                       mfi->mode = MTD_MODE_OTP_USER;
+               break;
+       default:
+               ret = -EINVAL;
+       case MTD_OTP_OFF:
+               break;
+       }
+       return ret;
+}
+#else
+# define otp_select_filemode(f,m)      -EOPNOTSUPP
+#endif
+
 static int mtd_ioctl(struct inode *inode, struct file *file,
                     u_int cmd, u_long arg)
 {
-       struct mtd_info *mtd = (struct mtd_info *)file->private_data;
+       struct mtd_file_info *mfi = file->private_data;
+       struct mtd_info *mtd = mfi->mtd;
+       void __user *argp = (void __user *)arg;
        int ret = 0;
        u_long size;
-       
+       struct mtd_info_user info;
+
        DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
 
        size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
        if (cmd & IOC_IN) {
-               ret = verify_area(VERIFY_READ, (char *)arg, size);
-               if (ret) return ret;
+               if (!access_ok(VERIFY_READ, argp, size))
+                       return -EFAULT;
        }
        if (cmd & IOC_OUT) {
-               ret = verify_area(VERIFY_WRITE, (char *)arg, size);
-               if (ret) return ret;
+               if (!access_ok(VERIFY_WRITE, argp, size))
+                       return -EFAULT;
        }
-       
+
        switch (cmd) {
        case MEMGETREGIONCOUNT:
-               if (copy_to_user((int *) arg, &(mtd->numeraseregions), sizeof(int)))
+               if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
                        return -EFAULT;
                break;
 
@@ -263,24 +401,27 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
        {
                struct region_info_user ur;
 
-               if (copy_from_user(     &ur, 
-                                       (struct region_info_user *)arg, 
-                                       sizeof(struct region_info_user))) {
+               if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
                        return -EFAULT;
-               }
 
                if (ur.regionindex >= mtd->numeraseregions)
                        return -EINVAL;
-               if (copy_to_user((struct mtd_erase_region_info *) arg, 
-                               &(mtd->eraseregions[ur.regionindex]),
+               if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
                                sizeof(struct mtd_erase_region_info)))
                        return -EFAULT;
                break;
        }
 
        case MEMGETINFO:
-               if (copy_to_user((struct mtd_info *)arg, mtd,
-                                sizeof(struct mtd_info_user)))
+               info.type       = mtd->type;
+               info.flags      = mtd->flags;
+               info.size       = mtd->size;
+               info.erasesize  = mtd->erasesize;
+               info.writesize  = mtd->writesize;
+               info.oobsize    = mtd->oobsize;
+               info.ecctype    = mtd->ecctype;
+               info.eccsize    = mtd->eccsize;
+               if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
                        return -EFAULT;
                break;
 
@@ -291,7 +432,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
                if(!(file->f_mode & 2))
                        return -EPERM;
 
-               erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
+               erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
                if (!erase)
                        ret = -ENOMEM;
                else {
@@ -300,20 +441,19 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
 
                        init_waitqueue_head(&waitq);
 
-                       memset (erase,0,sizeof(struct erase_info));
-                       if (copy_from_user(&erase->addr, (u_long *)arg,
-                                          2 * sizeof(u_long))) {
+                       if (copy_from_user(&erase->addr, argp,
+                                   sizeof(struct erase_info_user))) {
                                kfree(erase);
                                return -EFAULT;
                        }
                        erase->mtd = mtd;
-                       erase->callback = mtd_erase_callback;
+                       erase->callback = mtdchar_erase_callback;
                        erase->priv = (unsigned long)&waitq;
-                       
+
                        /*
                          FIXME: Allow INTERRUPTIBLE. Which means
                          not having the wait_queue head on the stack.
-                         
+
                          If the wq_head is on the stack, and we
                          leave because we got interrupted, then the
                          wq_head is no longer there when the
@@ -339,41 +479,51 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
        case MEMWRITEOOB:
        {
                struct mtd_oob_buf buf;
-               void *databuf;
-               ssize_t retlen;
-               
+               struct mtd_oob_ops ops;
+
                if(!(file->f_mode & 2))
                        return -EPERM;
 
-               if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
+               if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
                        return -EFAULT;
-               
-               if (buf.length > 0x4096)
+
+               if (buf.length > 4096)
                        return -EINVAL;
 
                if (!mtd->write_oob)
                        ret = -EOPNOTSUPP;
                else
-                       ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
+                       ret = access_ok(VERIFY_READ, buf.ptr,
+                                       buf.length) ? 0 : EFAULT;
 
                if (ret)
                        return ret;
 
-               databuf = kmalloc(buf.length, GFP_KERNEL);
-               if (!databuf)
+               ops.ooblen = buf.length;
+               ops.ooboffs = buf.start & (mtd->oobsize - 1);
+               ops.datbuf = NULL;
+               ops.mode = MTD_OOB_PLACE;
+
+               if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
+                       return -EINVAL;
+
+               ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
+               if (!ops.oobbuf)
                        return -ENOMEM;
-               
-               if (copy_from_user(databuf, buf.ptr, buf.length)) {
-                       kfree(databuf);
+
+               if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
+                       kfree(ops.oobbuf);
                        return -EFAULT;
                }
 
-               ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
+               buf.start &= ~(mtd->oobsize - 1);
+               ret = mtd->write_oob(mtd, buf.start, &ops);
 
-               if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
+               if (copy_to_user(argp + sizeof(uint32_t), &ops.oobretlen,
+                                sizeof(uint32_t)))
                        ret = -EFAULT;
 
-               kfree(databuf);
+               kfree(ops.oobbuf);
                break;
 
        }
@@ -381,75 +531,228 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
        case MEMREADOOB:
        {
                struct mtd_oob_buf buf;
-               void *databuf;
-               ssize_t retlen;
+               struct mtd_oob_ops ops;
 
-               if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
+               if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
                        return -EFAULT;
-               
-               if (buf.length > 0x4096)
+
+               if (buf.length > 4096)
                        return -EINVAL;
 
                if (!mtd->read_oob)
                        ret = -EOPNOTSUPP;
                else
-                       ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
-
+                       ret = access_ok(VERIFY_WRITE, buf.ptr,
+                                       buf.length) ? 0 : -EFAULT;
                if (ret)
                        return ret;
 
-               databuf = kmalloc(buf.length, GFP_KERNEL);
-               if (!databuf)
+               ops.ooblen = buf.length;
+               ops.ooboffs = buf.start & (mtd->oobsize - 1);
+               ops.datbuf = NULL;
+               ops.mode = MTD_OOB_PLACE;
+
+               if (ops.ooboffs && ops.len > (mtd->oobsize - ops.ooboffs))
+                       return -EINVAL;
+
+               ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
+               if (!ops.oobbuf)
                        return -ENOMEM;
-               
-               ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
 
-               if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
+               buf.start &= ~(mtd->oobsize - 1);
+               ret = mtd->read_oob(mtd, buf.start, &ops);
+
+               if (put_user(ops.oobretlen, (uint32_t __user *)argp))
                        ret = -EFAULT;
-               else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
+               else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
+                                                   ops.oobretlen))
                        ret = -EFAULT;
-               
-               kfree(databuf);
+
+               kfree(ops.oobbuf);
                break;
        }
 
        case MEMLOCK:
        {
-               unsigned long adrs[2];
+               struct erase_info_user info;
 
-               if (copy_from_user(adrs ,(void *)arg, 2* sizeof(unsigned long)))
+               if (copy_from_user(&info, argp, sizeof(info)))
                        return -EFAULT;
 
                if (!mtd->lock)
                        ret = -EOPNOTSUPP;
                else
-                       ret = mtd->lock(mtd, adrs[0], adrs[1]);
+                       ret = mtd->lock(mtd, info.start, info.length);
                break;
        }
 
        case MEMUNLOCK:
        {
-               unsigned long adrs[2];
+               struct erase_info_user info;
 
-               if (copy_from_user(adrs, (void *)arg, 2* sizeof(unsigned long)))
+               if (copy_from_user(&info, argp, sizeof(info)))
                        return -EFAULT;
 
                if (!mtd->unlock)
                        ret = -EOPNOTSUPP;
                else
-                       ret = mtd->unlock(mtd, adrs[0], adrs[1]);
+                       ret = mtd->unlock(mtd, info.start, info.length);
+               break;
+       }
+
+       /* Legacy interface */
+       case MEMGETOOBSEL:
+       {
+               struct nand_oobinfo oi;
+
+               if (!mtd->ecclayout)
+                       return -EOPNOTSUPP;
+               if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
+                       return -EINVAL;
+
+               oi.useecc = MTD_NANDECC_AUTOPLACE;
+               memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
+               memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
+                      sizeof(oi.oobfree));
+               oi.eccbytes = mtd->ecclayout->eccbytes;
+
+               if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
+                       return -EFAULT;
+               break;
+       }
+
+       case MEMGETBADBLOCK:
+       {
+               loff_t offs;
+
+               if (copy_from_user(&offs, argp, sizeof(loff_t)))
+                       return -EFAULT;
+               if (!mtd->block_isbad)
+                       ret = -EOPNOTSUPP;
+               else
+                       return mtd->block_isbad(mtd, offs);
+               break;
+       }
+
+       case MEMSETBADBLOCK:
+       {
+               loff_t offs;
+
+               if (copy_from_user(&offs, argp, sizeof(loff_t)))
+                       return -EFAULT;
+               if (!mtd->block_markbad)
+                       ret = -EOPNOTSUPP;
+               else
+                       return mtd->block_markbad(mtd, offs);
+               break;
+       }
+
+#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
+       case OTPSELECT:
+       {
+               int mode;
+               if (copy_from_user(&mode, argp, sizeof(int)))
+                       return -EFAULT;
+
+               mfi->mode = MTD_MODE_NORMAL;
+
+               ret = otp_select_filemode(mfi, mode);
+
+               file->f_pos = 0;
+               break;
+       }
+
+       case OTPGETREGIONCOUNT:
+       case OTPGETREGIONINFO:
+       {
+               struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
+               if (!buf)
+                       return -ENOMEM;
+               ret = -EOPNOTSUPP;
+               switch (mfi->mode) {
+               case MTD_MODE_OTP_FACTORY:
+                       if (mtd->get_fact_prot_info)
+                               ret = mtd->get_fact_prot_info(mtd, buf, 4096);
+                       break;
+               case MTD_MODE_OTP_USER:
+                       if (mtd->get_user_prot_info)
+                               ret = mtd->get_user_prot_info(mtd, buf, 4096);
+                       break;
+               default:
+                       break;
+               }
+               if (ret >= 0) {
+                       if (cmd == OTPGETREGIONCOUNT) {
+                               int nbr = ret / sizeof(struct otp_info);
+                               ret = copy_to_user(argp, &nbr, sizeof(int));
+                       } else
+                               ret = copy_to_user(argp, buf, ret);
+                       if (ret)
+                               ret = -EFAULT;
+               }
+               kfree(buf);
                break;
        }
 
-       case MEMSETOOBSEL:
+       case OTPLOCK:
        {
-               if (copy_from_user(&mtd->oobinfo ,(void *)arg, sizeof(struct nand_oobinfo)))
+               struct otp_info info;
+
+               if (mfi->mode != MTD_MODE_OTP_USER)
+                       return -EINVAL;
+               if (copy_from_user(&info, argp, sizeof(info)))
                        return -EFAULT;
+               if (!mtd->lock_user_prot_reg)
+                       return -EOPNOTSUPP;
+               ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
+               break;
+       }
+#endif
+
+       case ECCGETLAYOUT:
+       {
+               if (!mtd->ecclayout)
+                       return -EOPNOTSUPP;
+
+               if (copy_to_user(argp, mtd->ecclayout,
+                                sizeof(struct nand_ecclayout)))
+                       return -EFAULT;
+               break;
+       }
+
+       case ECCGETSTATS:
+       {
+               if (copy_to_user(argp, &mtd->ecc_stats,
+                                sizeof(struct mtd_ecc_stats)))
+                       return -EFAULT;
+               break;
+       }
+
+       case MTDFILEMODE:
+       {
+               mfi->mode = 0;
+
+               switch(arg) {
+               case MTD_MODE_OTP_FACTORY:
+               case MTD_MODE_OTP_USER:
+                       ret = otp_select_filemode(mfi, arg);
+                       break;
+
+               case MTD_MODE_RAW:
+                       if (!mtd->read_oob || !mtd->write_oob)
+                               return -EOPNOTSUPP;
+                       mfi->mode = arg;
+
+               case MTD_MODE_NORMAL:
+                       break;
+               default:
+                       ret = -EINVAL;
+               }
+               file->f_pos = 0;
                break;
        }
-               
+
        default:
-               DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
                ret = -ENOTTY;
        }
 
@@ -466,30 +769,6 @@ static struct file_operations mtd_fops = {
        .release        = mtd_close,
 };
 
-
-#ifdef CONFIG_DEVFS_FS
-/* Notification that a new device has been added. Create the devfs entry for
- * it. */
-
-static void mtd_notify_add(struct mtd_info* mtd)
-{
-       if (!mtd)
-               return;
-       devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
-                       S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
-       devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
-                       S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
-}
-
-static void mtd_notify_remove(struct mtd_info* mtd)
-{
-       if (!mtd)
-               return;
-       devfs_remove("mtd/%d", mtd->index);
-       devfs_remove("mtd/%dro", mtd->index);
-}
-#endif
-
 static int __init init_mtdchar(void)
 {
        if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
@@ -498,20 +777,22 @@ static int __init init_mtdchar(void)
                return -EAGAIN;
        }
 
-#ifdef CONFIG_DEVFS_FS
-       devfs_mk_dir("mtd");
+       mtd_class = class_create(THIS_MODULE, "mtd");
+
+       if (IS_ERR(mtd_class)) {
+               printk(KERN_ERR "Error creating mtd class.\n");
+               unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
+               return PTR_ERR(mtd_class);
+       }
 
        register_mtd_user(&notifier);
-#endif
        return 0;
 }
 
 static void __exit cleanup_mtdchar(void)
 {
-#ifdef CONFIG_DEVFS_FS
        unregister_mtd_user(&notifier);
-       devfs_remove("mtd");
-#endif
+       class_destroy(mtd_class);
        unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
 }