Merge branch 'topic/pcm-delay' into for-linus
[safe/jmp/linux-2.6] / sound / core / info.c
index 340332c..35df614 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Information interface for ALSA driver
- *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
+ *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  *
  *
  *   This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  */
 
-#include <sound/driver.h>
 #include <linux/init.h>
 #include <linux/time.h>
+#include <linux/mm.h>
 #include <linux/smp_lock.h>
 #include <linux/string.h>
 #include <sound/core.h>
@@ -78,6 +78,7 @@ struct snd_info_private_data {
 
 static int snd_info_version_init(void);
 static int snd_info_version_done(void);
+static void snd_info_disconnect(struct snd_info_entry *entry);
 
 
 /* resize the proc r/w buffer */
@@ -119,7 +120,10 @@ int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...)
        len = buffer->len - buffer->size;
        va_start(args, fmt);
        for (;;) {
-               res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, args);
+               va_list ap;
+               va_copy(ap, args);
+               res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap);
+               va_end(ap);
                if (res < len)
                        break;
                err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE);
@@ -150,11 +154,6 @@ EXPORT_SYMBOL(snd_seq_root);
 struct snd_info_entry *snd_oss_root;
 #endif
 
-static inline void snd_info_entry_prepare(struct proc_dir_entry *de)
-{
-       de->owner = THIS_MODULE;
-}
-
 static void snd_remove_proc_entry(struct proc_dir_entry *parent,
                                  struct proc_dir_entry *de)
 {
@@ -174,15 +173,15 @@ static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
        switch (entry->content) {
        case SNDRV_INFO_CONTENT_TEXT:
                switch (orig) {
-               case 0: /* SEEK_SET */
+               case SEEK_SET:
                        file->f_pos = offset;
                        ret = file->f_pos;
                        goto out;
-               case 1: /* SEEK_CUR */
+               case SEEK_CUR:
                        file->f_pos += offset;
                        ret = file->f_pos;
                        goto out;
-               case 2: /* SEEK_END */
+               case SEEK_END:
                default:
                        ret = -EINVAL;
                        goto out;
@@ -213,7 +212,8 @@ static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
        loff_t pos;
 
        data = file->private_data;
-       snd_assert(data != NULL, return -ENXIO);
+       if (snd_BUG_ON(!data))
+               return -ENXIO;
        pos = *offset;
        if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
                return -EIO;
@@ -254,7 +254,8 @@ static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer
        loff_t pos;
 
        data = file->private_data;
-       snd_assert(data != NULL, return -ENXIO);
+       if (snd_BUG_ON(!data))
+               return -ENXIO;
        entry = data->entry;
        pos = *offset;
        if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
@@ -304,7 +305,7 @@ static int snd_info_entry_open(struct inode *inode, struct file *file)
        mutex_lock(&info_mutex);
        p = PDE(inode);
        entry = p == NULL ? NULL : (struct snd_info_entry *)p->data;
-       if (entry == NULL || entry->disconnected) {
+       if (entry == NULL || ! entry->p) {
                mutex_unlock(&info_mutex);
                return -ENODEV;
        }
@@ -484,7 +485,7 @@ static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
 
 static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
 {
-       struct inode *inode = file->f_dentry->d_inode;
+       struct inode *inode = file->f_path.dentry->d_inode;
        struct snd_info_private_data *data;
        struct snd_info_entry *entry;
 
@@ -503,7 +504,7 @@ static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
        return -ENXIO;
 }
 
-static struct file_operations snd_info_entry_operations =
+static const struct file_operations snd_info_entry_operations =
 {
        .owner =                THIS_MODULE,
        .llseek =               snd_info_entry_llseek,
@@ -516,32 +517,11 @@ static struct file_operations snd_info_entry_operations =
        .release =              snd_info_entry_release,
 };
 
-/**
- * snd_create_proc_entry - create a procfs entry
- * @name: the name of the proc file
- * @mode: the file permission bits, S_Ixxx
- * @parent: the parent proc-directory entry
- *
- * Creates a new proc file entry with the given name and permission
- * on the given directory.
- *
- * Returns the pointer of new instance or NULL on failure.
- */
-static struct proc_dir_entry *snd_create_proc_entry(const char *name, mode_t mode,
-                                                   struct proc_dir_entry *parent)
-{
-       struct proc_dir_entry *p;
-       p = create_proc_entry(name, mode, parent);
-       if (p)
-               snd_info_entry_prepare(p);
-       return p;
-}
-
 int __init snd_info_init(void)
 {
        struct proc_dir_entry *p;
 
-       p = snd_create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, &proc_root);
+       p = create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL);
        if (p == NULL)
                return -ENOMEM;
        snd_proc_root = p;
@@ -586,12 +566,12 @@ int __exit snd_info_done(void)
        snd_info_version_done();
        if (snd_proc_root) {
 #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE)
-               snd_info_unregister(snd_seq_root);
+               snd_info_free_entry(snd_seq_root);
 #endif
 #ifdef CONFIG_SND_OSSEMUL
-               snd_info_unregister(snd_oss_root);
+               snd_info_free_entry(snd_oss_root);
 #endif
-               snd_remove_proc_entry(&proc_root, snd_proc_root);
+               snd_remove_proc_entry(NULL, snd_proc_root);
        }
        return 0;
 }
@@ -610,7 +590,8 @@ int snd_info_card_create(struct snd_card *card)
        char str[8];
        struct snd_info_entry *entry;
 
-       snd_assert(card != NULL, return -ENXIO);
+       if (snd_BUG_ON(!card))
+               return -ENXIO;
 
        sprintf(str, "card%i", card->number);
        if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL)
@@ -632,7 +613,8 @@ int snd_info_card_register(struct snd_card *card)
 {
        struct proc_dir_entry *p;
 
-       snd_assert(card != NULL, return -ENXIO);
+       if (snd_BUG_ON(!card))
+               return -ENXIO;
 
        if (!strcmp(card->id, card->proc_root->name))
                return 0;
@@ -645,20 +627,50 @@ int snd_info_card_register(struct snd_card *card)
 }
 
 /*
+ * called on card->id change
+ */
+void snd_info_card_id_change(struct snd_card *card)
+{
+       mutex_lock(&info_mutex);
+       if (card->proc_root_link) {
+               snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
+               card->proc_root_link = NULL;
+       }
+       if (strcmp(card->id, card->proc_root->name))
+               card->proc_root_link = proc_symlink(card->id,
+                                                   snd_proc_root,
+                                                   card->proc_root->name);
+       mutex_unlock(&info_mutex);
+}
+
+/*
  * de-register the card proc file
  * called from init.c
  */
-int snd_info_card_free(struct snd_card *card)
+void snd_info_card_disconnect(struct snd_card *card)
 {
-       snd_assert(card != NULL, return -ENXIO);
+       if (!card)
+               return;
+       mutex_lock(&info_mutex);
        if (card->proc_root_link) {
                snd_remove_proc_entry(snd_proc_root, card->proc_root_link);
                card->proc_root_link = NULL;
        }
-       if (card->proc_root) {
-               snd_info_unregister(card->proc_root);
-               card->proc_root = NULL;
-       }
+       if (card->proc_root)
+               snd_info_disconnect(card->proc_root);
+       mutex_unlock(&info_mutex);
+}
+
+/*
+ * release the card proc file resources
+ * called from init.c
+ */
+int snd_info_card_free(struct snd_card *card)
+{
+       if (!card)
+               return 0;
+       snd_info_free_entry(card->proc_root);
+       card->proc_root = NULL;
        return 0;
 }
 
@@ -767,6 +779,8 @@ static struct snd_info_entry *snd_info_create_entry(const char *name)
        entry->mode = S_IFREG | S_IRUGO;
        entry->content = SNDRV_INFO_CONTENT_TEXT;
        mutex_init(&entry->access);
+       INIT_LIST_HEAD(&entry->children);
+       INIT_LIST_HEAD(&entry->list);
        return entry;
 }
 
@@ -819,30 +833,35 @@ struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
 
 EXPORT_SYMBOL(snd_info_create_card_entry);
 
-static int snd_info_dev_free_entry(struct snd_device *device)
+static void snd_info_disconnect(struct snd_info_entry *entry)
 {
-       struct snd_info_entry *entry = device->device_data;
-       snd_info_free_entry(entry);
-       return 0;
-}
+       struct list_head *p, *n;
+       struct proc_dir_entry *root;
 
-static int snd_info_dev_register_entry(struct snd_device *device)
-{
-       struct snd_info_entry *entry = device->device_data;
-       return snd_info_register(entry);
+       list_for_each_safe(p, n, &entry->children) {
+               snd_info_disconnect(list_entry(p, struct snd_info_entry, list));
+       }
+
+       if (! entry->p)
+               return;
+       list_del_init(&entry->list);
+       root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
+       snd_BUG_ON(!root);
+       snd_remove_proc_entry(root, entry->p);
+       entry->p = NULL;
 }
 
-static int snd_info_dev_disconnect_entry(struct snd_device *device)
+static int snd_info_dev_free_entry(struct snd_device *device)
 {
        struct snd_info_entry *entry = device->device_data;
-       entry->disconnected = 1;
+       snd_info_free_entry(entry);
        return 0;
 }
 
-static int snd_info_dev_unregister_entry(struct snd_device *device)
+static int snd_info_dev_register_entry(struct snd_device *device)
 {
        struct snd_info_entry *entry = device->device_data;
-       return snd_info_unregister(entry);
+       return snd_info_register(entry);
 }
 
 /**
@@ -871,8 +890,7 @@ int snd_card_proc_new(struct snd_card *card, const char *name,
        static struct snd_device_ops ops = {
                .dev_free = snd_info_dev_free_entry,
                .dev_register = snd_info_dev_register_entry,
-               .dev_disconnect = snd_info_dev_disconnect_entry,
-               .dev_unregister = snd_info_dev_unregister_entry
+               /* disconnect is done via snd_info_card_disconnect() */
        };
        struct snd_info_entry *entry;
        int err;
@@ -901,6 +919,11 @@ void snd_info_free_entry(struct snd_info_entry * entry)
 {
        if (entry == NULL)
                return;
+       if (entry->p) {
+               mutex_lock(&info_mutex);
+               snd_info_disconnect(entry);
+               mutex_unlock(&info_mutex);
+       }
        kfree(entry->name);
        if (entry->private_free)
                entry->private_free(entry);
@@ -921,52 +944,28 @@ int snd_info_register(struct snd_info_entry * entry)
 {
        struct proc_dir_entry *root, *p = NULL;
 
-       snd_assert(entry != NULL, return -ENXIO);
+       if (snd_BUG_ON(!entry))
+               return -ENXIO;
        root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
        mutex_lock(&info_mutex);
-       p = snd_create_proc_entry(entry->name, entry->mode, root);
+       p = create_proc_entry(entry->name, entry->mode, root);
        if (!p) {
                mutex_unlock(&info_mutex);
                return -ENOMEM;
        }
-       p->owner = entry->module;
        if (!S_ISDIR(entry->mode))
                p->proc_fops = &snd_info_entry_operations;
        p->size = entry->size;
        p->data = entry;
        entry->p = p;
+       if (entry->parent)
+               list_add_tail(&entry->list, &entry->parent->children);
        mutex_unlock(&info_mutex);
        return 0;
 }
 
 EXPORT_SYMBOL(snd_info_register);
 
-/**
- * snd_info_unregister - de-register the info entry
- * @entry: the info entry
- *
- * De-registers the info entry and releases the instance.
- *
- * Returns zero if successful, or a negative error code on failure.
- */
-int snd_info_unregister(struct snd_info_entry * entry)
-{
-       struct proc_dir_entry *root;
-
-       if (! entry)
-               return 0;
-       snd_assert(entry->p != NULL, return -ENXIO);
-       root = entry->parent == NULL ? snd_proc_root : entry->parent->p;
-       snd_assert(root, return -ENXIO);
-       mutex_lock(&info_mutex);
-       snd_remove_proc_entry(root, entry->p);
-       mutex_unlock(&info_mutex);
-       snd_info_free_entry(entry);
-       return 0;
-}
-
-EXPORT_SYMBOL(snd_info_unregister);
-
 /*
 
  */
@@ -999,8 +998,7 @@ static int __init snd_info_version_init(void)
 
 static int __exit snd_info_version_done(void)
 {
-       if (snd_info_version_entry)
-               snd_info_unregister(snd_info_version_entry);
+       snd_info_free_entry(snd_info_version_entry);
        return 0;
 }