fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / mtd / mtdcore.c
index dade02a..7070110 100644 (file)
@@ -6,7 +6,6 @@
  *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/timer.h>
 #include <linux/major.h>
 #include <linux/fs.h>
+#include <linux/err.h>
 #include <linux/ioctl.h>
 #include <linux/init.h>
 #include <linux/mtd/compatmac.h>
-#ifdef CONFIG_PROC_FS
 #include <linux/proc_fs.h>
-#endif
 
 #include <linux/mtd/mtd.h>
 
 /* These are exported solely for the purpose of mtd_blkdevs.c. You
    should not use them for _anything_ else */
-DECLARE_MUTEX(mtd_table_mutex);
+DEFINE_MUTEX(mtd_table_mutex);
 struct mtd_info *mtd_table[MAX_MTD_DEVICES];
 
 EXPORT_SYMBOL_GPL(mtd_table_mutex);
@@ -49,7 +47,8 @@ int add_mtd_device(struct mtd_info *mtd)
 {
        int i;
 
-       down(&mtd_table_mutex);
+       BUG_ON(mtd->writesize == 0);
+       mutex_lock(&mtd_table_mutex);
 
        for (i=0; i < MAX_MTD_DEVICES; i++)
                if (!mtd_table[i]) {
@@ -59,6 +58,16 @@ int add_mtd_device(struct mtd_info *mtd)
                        mtd->index = i;
                        mtd->usecount = 0;
 
+                       /* Some chips always power up locked. Unlock them now */
+                       if ((mtd->flags & MTD_WRITEABLE)
+                           && (mtd->flags & MTD_STUPID_LOCK) && mtd->unlock) {
+                               if (mtd->unlock(mtd, 0, mtd->size))
+                                       printk(KERN_WARNING
+                                              "%s: unlock failed, "
+                                              "writes may not work\n",
+                                              mtd->name);
+                       }
+
                        DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
                        /* No need to get a refcount on the module containing
                           the notifier, since we hold the mtd_table_mutex */
@@ -67,7 +76,7 @@ int add_mtd_device(struct mtd_info *mtd)
                                not->add(mtd);
                        }
 
-                       up(&mtd_table_mutex);
+                       mutex_unlock(&mtd_table_mutex);
                        /* We _know_ we aren't being removed, because
                           our caller is still holding us here. So none
                           of this try_ nonsense, and no bitching about it
@@ -76,7 +85,7 @@ int add_mtd_device(struct mtd_info *mtd)
                        return 0;
                }
 
-       up(&mtd_table_mutex);
+       mutex_unlock(&mtd_table_mutex);
        return 1;
 }
 
@@ -94,7 +103,7 @@ int del_mtd_device (struct mtd_info *mtd)
 {
        int ret;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
 
        if (mtd_table[mtd->index] != mtd) {
                ret = -ENODEV;
@@ -118,7 +127,7 @@ int del_mtd_device (struct mtd_info *mtd)
                ret = 0;
        }
 
-       up(&mtd_table_mutex);
+       mutex_unlock(&mtd_table_mutex);
        return ret;
 }
 
@@ -135,7 +144,7 @@ void register_mtd_user (struct mtd_notifier *new)
 {
        int i;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
 
        list_add(&new->list, &mtd_notifiers);
 
@@ -145,7 +154,7 @@ void register_mtd_user (struct mtd_notifier *new)
                if (mtd_table[i])
                        new->add(mtd_table[i]);
 
-       up(&mtd_table_mutex);
+       mutex_unlock(&mtd_table_mutex);
 }
 
 /**
@@ -162,7 +171,7 @@ int unregister_mtd_user (struct mtd_notifier *old)
 {
        int i;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
 
        module_put(THIS_MODULE);
 
@@ -171,7 +180,7 @@ int unregister_mtd_user (struct mtd_notifier *old)
                        old->remove(mtd_table[i]);
 
        list_del(&old->list);
-       up(&mtd_table_mutex);
+       mutex_unlock(&mtd_table_mutex);
        return 0;
 }
 
@@ -184,16 +193,16 @@ int unregister_mtd_user (struct mtd_notifier *old)
  *     Given a number and NULL address, return the num'th entry in the device
  *     table, if any.  Given an address and num == -1, search the device table
  *     for a device with that address and return if it's still present. Given
- *     both, return the num'th driver only if its address matches. Return NULL
- *     if not.
+ *     both, return the num'th driver only if its address matches. Return
+ *     error code if not.
  */
 
 struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
 {
        struct mtd_info *ret = NULL;
-       int i;
+       int i, err = -ENODEV;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
 
        if (num == -1) {
                for (i=0; i< MAX_MTD_DEVICES; i++)
@@ -205,30 +214,91 @@ struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
                        ret = NULL;
        }
 
-       if (ret && !try_module_get(ret->owner))
-               ret = NULL;
+       if (!ret)
+               goto out_unlock;
 
-       if (ret)
-               ret->usecount++;
+       if (!try_module_get(ret->owner))
+               goto out_unlock;
 
-       up(&mtd_table_mutex);
+       if (ret->get_device) {
+               err = ret->get_device(ret);
+               if (err)
+                       goto out_put;
+       }
+
+       ret->usecount++;
+       mutex_unlock(&mtd_table_mutex);
        return ret;
+
+out_put:
+       module_put(ret->owner);
+out_unlock:
+       mutex_unlock(&mtd_table_mutex);
+       return ERR_PTR(err);
+}
+
+/**
+ *     get_mtd_device_nm - obtain a validated handle for an MTD device by
+ *     device name
+ *     @name: MTD device name to open
+ *
+ *     This function returns MTD device description structure in case of
+ *     success and an error code in case of failure.
+ */
+
+struct mtd_info *get_mtd_device_nm(const char *name)
+{
+       int i, err = -ENODEV;
+       struct mtd_info *mtd = NULL;
+
+       mutex_lock(&mtd_table_mutex);
+
+       for (i = 0; i < MAX_MTD_DEVICES; i++) {
+               if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
+                       mtd = mtd_table[i];
+                       break;
+               }
+       }
+
+       if (!mtd)
+               goto out_unlock;
+
+       if (!try_module_get(mtd->owner))
+               goto out_unlock;
+
+       if (mtd->get_device) {
+               err = mtd->get_device(mtd);
+               if (err)
+                       goto out_put;
+       }
+
+       mtd->usecount++;
+       mutex_unlock(&mtd_table_mutex);
+       return mtd;
+
+out_put:
+       module_put(mtd->owner);
+out_unlock:
+       mutex_unlock(&mtd_table_mutex);
+       return ERR_PTR(err);
 }
 
 void put_mtd_device(struct mtd_info *mtd)
 {
        int c;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
        c = --mtd->usecount;
-       up(&mtd_table_mutex);
+       if (mtd->put_device)
+               mtd->put_device(mtd);
+       mutex_unlock(&mtd_table_mutex);
        BUG_ON(c < 0);
 
        module_put(mtd->owner);
 }
 
 /* default_mtd_writev - default mtd writev method for MTD devices that
- *                     dont implement their own
+ *                     don't implement their own
  */
 
 int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
@@ -256,50 +326,20 @@ int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
        return ret;
 }
 
+EXPORT_SYMBOL_GPL(add_mtd_device);
+EXPORT_SYMBOL_GPL(del_mtd_device);
+EXPORT_SYMBOL_GPL(get_mtd_device);
+EXPORT_SYMBOL_GPL(get_mtd_device_nm);
+EXPORT_SYMBOL_GPL(put_mtd_device);
+EXPORT_SYMBOL_GPL(register_mtd_user);
+EXPORT_SYMBOL_GPL(unregister_mtd_user);
+EXPORT_SYMBOL_GPL(default_mtd_writev);
 
-/* default_mtd_readv - default mtd readv method for MTD devices that dont
- *                    implement their own
- */
-
-int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
-                     unsigned long count, loff_t from, size_t *retlen)
-{
-       unsigned long i;
-       size_t totlen = 0, thislen;
-       int ret = 0;
-
-       if(!mtd->read) {
-               ret = -EIO;
-       } else {
-               for (i=0; i<count; i++) {
-                       if (!vecs[i].iov_len)
-                               continue;
-                       ret = mtd->read(mtd, from, vecs[i].iov_len, &thislen, vecs[i].iov_base);
-                       totlen += thislen;
-                       if (ret || thislen != vecs[i].iov_len)
-                               break;
-                       from += vecs[i].iov_len;
-               }
-       }
-       if (retlen)
-               *retlen = totlen;
-       return ret;
-}
-
-
-EXPORT_SYMBOL(add_mtd_device);
-EXPORT_SYMBOL(del_mtd_device);
-EXPORT_SYMBOL(get_mtd_device);
-EXPORT_SYMBOL(put_mtd_device);
-EXPORT_SYMBOL(register_mtd_user);
-EXPORT_SYMBOL(unregister_mtd_user);
-EXPORT_SYMBOL(default_mtd_writev);
-EXPORT_SYMBOL(default_mtd_readv);
+#ifdef CONFIG_PROC_FS
 
 /*====================================================================*/
 /* Support for /proc/mtd */
 
-#ifdef CONFIG_PROC_FS
 static struct proc_dir_entry *proc_mtd;
 
 static inline int mtd_proc_info (char *buf, int i)
@@ -319,7 +359,7 @@ static int mtd_read_proc (char *page, char **start, off_t off, int count,
        int len, l, i;
         off_t   begin = 0;
 
-       down(&mtd_table_mutex);
+       mutex_lock(&mtd_table_mutex);
 
        len = sprintf(page, "dev:    size   erasesize  name\n");
         for (i=0; i< MAX_MTD_DEVICES; i++) {
@@ -337,38 +377,34 @@ static int mtd_read_proc (char *page, char **start, off_t off, int count,
         *eof = 1;
 
 done:
-       up(&mtd_table_mutex);
+       mutex_unlock(&mtd_table_mutex);
         if (off >= len+begin)
                 return 0;
         *start = page + (off-begin);
         return ((count < begin+len-off) ? count : begin+len-off);
 }
 
-#endif /* CONFIG_PROC_FS */
-
 /*====================================================================*/
 /* Init code */
 
 static int __init init_mtd(void)
 {
-#ifdef CONFIG_PROC_FS
        if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
                proc_mtd->read_proc = mtd_read_proc;
-#endif
        return 0;
 }
 
 static void __exit cleanup_mtd(void)
 {
-#ifdef CONFIG_PROC_FS
         if (proc_mtd)
                remove_proc_entry( "mtd", NULL);
-#endif
 }
 
 module_init(init_mtd);
 module_exit(cleanup_mtd);
 
+#endif /* CONFIG_PROC_FS */
+
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");