#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>
#include <sound/initval.h>
#include <linux/kmod.h>
#include <linux/devfs_fs_kernel.h>
+#include <linux/mutex.h>
#define SNDRV_OS_MINORS 256
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;
#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)
.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;
snd_assert(minor >= 0 && minor < SNDRV_OS_MINORS, return -EINVAL);
return minor;
}
+#endif
/**
* snd_register_device - Register the ALSA device file for the card
* @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.
* 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)
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)
device = card->dev;
class_device_create(sound_class, NULL, MKDEV(major, minor), device, "%s", name);
- up(&sound_mutex);
+ mutex_unlock(&sound_mutex);
return 0;
}
*/
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;
}
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
*/
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)
snd_info_unregister(snd_minor_info_entry);
return 0;
}
+#endif /* CONFIG_PROC_FS */
/*
* INIT PART
#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);