[ALSA] Clean up PCM codes (take 2)
[safe/jmp/linux-2.6] / sound / core / sound.c
index a509f49..108e430 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/time.h>
+#include <linux/device.h>
 #include <linux/moduleparam.h>
 #include <sound/core.h>
 #include <sound/minors.h>
@@ -32,6 +33,7 @@
 #include <sound/initval.h>
 #include <linux/kmod.h>
 #include <linux/devfs_fs_kernel.h>
+#include <linux/mutex.h>
 
 #define SNDRV_OS_MINORS 256
 
@@ -60,8 +62,7 @@ MODULE_ALIAS_CHARDEV_MAJOR(CONFIG_SND_MAJOR);
 int snd_ecards_limit;
 
 static struct snd_minor *snd_minors[SNDRV_OS_MINORS];
-
-static DECLARE_MUTEX(sound_mutex);
+static DEFINE_MUTEX(sound_mutex);
 
 extern struct class *sound_class;
 
@@ -107,31 +108,61 @@ static void snd_request_other(int minor)
 
 #endif                         /* request_module support */
 
+/**
+ * snd_lookup_minor_data - get user data of a registered device
+ * @minor: the minor number
+ * @type: device type (SNDRV_DEVICE_TYPE_XXX)
+ *
+ * Checks that a minor device with the specified type is registered, and returns
+ * its user data pointer.
+ */
+void *snd_lookup_minor_data(unsigned int minor, int type)
+{
+       struct snd_minor *mreg;
+       void *private_data;
+
+       if (minor >= ARRAY_SIZE(snd_minors))
+               return NULL;
+       mutex_lock(&sound_mutex);
+       mreg = snd_minors[minor];
+       if (mreg && mreg->type == type)
+               private_data = mreg->private_data;
+       else
+               private_data = NULL;
+       mutex_unlock(&sound_mutex);
+       return private_data;
+}
+
 static int snd_open(struct inode *inode, struct file *file)
 {
-       int minor = iminor(inode);
-       int card = SNDRV_MINOR_CARD(minor);
-       int dev = SNDRV_MINOR_DEVICE(minor);
+       unsigned int minor = iminor(inode);
        struct snd_minor *mptr = NULL;
-       struct file_operations *old_fops;
+       const struct file_operations *old_fops;
        int err = 0;
 
-       if (dev != SNDRV_MINOR_GLOBAL) {
-               if (snd_cards[card] == NULL) {
+       if (minor >= ARRAY_SIZE(snd_minors))
+               return -ENODEV;
+       mptr = snd_minors[minor];
+       if (mptr == NULL) {
 #ifdef CONFIG_KMOD
-                       snd_request_card(card);
+               int dev = SNDRV_MINOR_DEVICE(minor);
+               if (dev == SNDRV_MINOR_CONTROL) {
+                       /* /dev/aloadC? */
+                       int card = SNDRV_MINOR_CARD(minor);
                        if (snd_cards[card] == NULL)
-#endif
-                               return -ENODEV;
-               }
-       } else {
-#ifdef CONFIG_KMOD
-               if ((mptr = snd_minors[minor]) == NULL)
+                               snd_request_card(card);
+               } else if (dev == SNDRV_MINOR_GLOBAL) {
+                       /* /dev/aloadSEQ */
                        snd_request_other(minor);
+               }
+#ifndef CONFIG_SND_DYNAMIC_MINORS
+               /* /dev/snd/{controlC?,seq} */
+               mptr = snd_minors[minor];
+               if (mptr == NULL)
+#endif
 #endif
+                       return -ENODEV;
        }
-       if (mptr == NULL && (mptr = snd_minors[minor]) == NULL)
-               return -ENODEV;
        old_fops = file->f_op;
        file->f_op = fops_get(mptr->f_ops);
        if (file->f_op->open)
@@ -150,6 +181,22 @@ static struct file_operations snd_fops =
        .open =         snd_open
 };
 
+#ifdef CONFIG_SND_DYNAMIC_MINORS
+static int snd_find_free_minor(void)
+{
+       int minor;
+
+       for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor) {
+               /* skip minors still used statically for autoloading devices */
+               if (SNDRV_MINOR_DEVICE(minor) == SNDRV_MINOR_CONTROL ||
+                   minor == SNDRV_MINOR_SEQUENCER)
+                       continue;
+               if (!snd_minors[minor])
+                       return minor;
+       }
+       return -EBUSY;
+}
+#else
 static int snd_kernel_minor(int type, struct snd_card *card, int dev)
 {
        int minor;
@@ -176,6 +223,7 @@ static int snd_kernel_minor(int type, struct snd_card *card, int dev)
        snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
        return minor;
 }
+#endif
 
 /**
  * snd_register_device - Register the ALSA device file for the card
@@ -183,6 +231,7 @@ static int snd_kernel_minor(int type, struct snd_card *card, int dev)
  * @card: the card instance
  * @dev: the device index
  * @f_ops: the file operations
+ * @private_data: user pointer for f_ops->open()
  * @name: the device file name
  *
  * Registers an ALSA device file for the given card.
@@ -191,14 +240,13 @@ static int snd_kernel_minor(int type, struct snd_card *card, int dev)
  * Retrurns zero if successful, or a negative error code on failure.
  */
 int snd_register_device(int type, struct snd_card *card, int dev,
-                       struct file_operations *f_ops, const char *name)
+                       const struct file_operations *f_ops, void *private_data,
+                       const char *name)
 {
-       int minor = snd_kernel_minor(type, card, dev);
+       int minor;
        struct snd_minor *preg;
        struct device *device = NULL;
 
-       if (minor < 0)
-               return minor;
        snd_assert(name, return -EINVAL);
        preg = kmalloc(sizeof(struct snd_minor) + strlen(name) + 1, GFP_KERNEL);
        if (preg == NULL)
@@ -207,12 +255,20 @@ int snd_register_device(int type, struct snd_card *card, int dev,
        preg->card = card ? card->number : -1;
        preg->device = dev;
        preg->f_ops = f_ops;
+       preg->private_data = private_data;
        strcpy(preg->name, name);
-       down(&sound_mutex);
-       if (snd_minors[minor]) {
-               up(&sound_mutex);
+       mutex_lock(&sound_mutex);
+#ifdef CONFIG_SND_DYNAMIC_MINORS
+       minor = snd_find_free_minor();
+#else
+       minor = snd_kernel_minor(type, card, dev);
+       if (minor >= 0 && snd_minors[minor])
+               minor = -EBUSY;
+#endif
+       if (minor < 0) {
+               mutex_unlock(&sound_mutex);
                kfree(preg);
-               return -EBUSY;
+               return minor;
        }
        snd_minors[minor] = preg;
        if (type != SNDRV_DEVICE_TYPE_CONTROL || preg->card >= cards_limit)
@@ -221,7 +277,7 @@ int snd_register_device(int type, struct snd_card *card, int dev,
                device = card->dev;
        class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
 
-       up(&sound_mutex);
+       mutex_unlock(&sound_mutex);
        return 0;
 }
 
@@ -238,14 +294,19 @@ int snd_register_device(int type, struct snd_card *card, int dev,
  */
 int snd_unregister_device(int type, struct snd_card *card, int dev)
 {
-       int minor = snd_kernel_minor(type, card, dev);
+       int cardnum, minor;
        struct snd_minor *mptr;
 
-       if (minor < 0)
-               return minor;
-       down(&sound_mutex);
-       if ((mptr = snd_minors[minor]) == NULL) {
-               up(&sound_mutex);
+       cardnum = card ? card->number : -1;
+       mutex_lock(&sound_mutex);
+       for (minor = 0; minor < ARRAY_SIZE(snd_minors); ++minor)
+               if ((mptr = snd_minors[minor]) != NULL &&
+                   mptr->type == type &&
+                   mptr->card == cardnum &&
+                   mptr->device == dev)
+                       break;
+       if (minor == ARRAY_SIZE(snd_minors)) {
+               mutex_unlock(&sound_mutex);
                return -EINVAL;
        }
 
@@ -255,11 +316,12 @@ int snd_unregister_device(int type, struct snd_card *card, int dev)
        class_device_destroy(sound_class, MKDEV(major, minor));
 
        snd_minors[minor] = NULL;
-       up(&sound_mutex);
+       mutex_unlock(&sound_mutex);
        kfree(mptr);
        return 0;
 }
 
+#ifdef CONFIG_PROC_FS
 /*
  *  INFO PART
  */
@@ -293,24 +355,24 @@ static void snd_minor_info_read(struct snd_info_entry *entry, struct snd_info_bu
        int minor;
        struct snd_minor *mptr;
 
-       down(&sound_mutex);
+       mutex_lock(&sound_mutex);
        for (minor = 0; minor < SNDRV_OS_MINORS; ++minor) {
                if (!(mptr = snd_minors[minor]))
                        continue;
                if (mptr->card >= 0) {
                        if (mptr->device >= 0)
-                               snd_iprintf(buffer, "%3i: [%i-%2i]: %s\n",
+                               snd_iprintf(buffer, "%3i: [%2i-%2i]: %s\n",
                                            minor, mptr->card, mptr->device,
                                            snd_device_type_name(mptr->type));
                        else
-                               snd_iprintf(buffer, "%3i: [%i]   : %s\n",
+                               snd_iprintf(buffer, "%3i: [%2i]   : %s\n",
                                            minor, mptr->card,
                                            snd_device_type_name(mptr->type));
                } else
-                       snd_iprintf(buffer, "%3i:       : %s\n", minor,
+                       snd_iprintf(buffer, "%3i:        : %s\n", minor,
                                    snd_device_type_name(mptr->type));
        }
-       up(&sound_mutex);
+       mutex_unlock(&sound_mutex);
 }
 
 int __init snd_minor_info_init(void)
@@ -336,6 +398,7 @@ int __exit snd_minor_info_done(void)
                snd_info_unregister(snd_minor_info_entry);
        return 0;
 }
+#endif /* CONFIG_PROC_FS */
 
 /*
  *  INIT PART
@@ -392,9 +455,11 @@ EXPORT_SYMBOL(snd_request_card);
 #endif
 EXPORT_SYMBOL(snd_register_device);
 EXPORT_SYMBOL(snd_unregister_device);
+EXPORT_SYMBOL(snd_lookup_minor_data);
 #if defined(CONFIG_SND_OSSEMUL)
 EXPORT_SYMBOL(snd_register_oss_device);
 EXPORT_SYMBOL(snd_unregister_oss_device);
+EXPORT_SYMBOL(snd_lookup_oss_minor_data);
 #endif
   /* memory.c */
 EXPORT_SYMBOL(copy_to_user_fromio);