Merge branches 'release', 'asus', 'sony-laptop' and 'thinkpad' into release
[pandora-kernel.git] / sound / core / control.c
index 0c7bcd6..01a1a5a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Routines for driver control interface
- *  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
  *
  */
 
-#include <sound/driver.h>
 #include <linux/threads.h>
 #include <linux/interrupt.h>
-#include <linux/smp_lock.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/time.h>
@@ -94,21 +92,21 @@ static int snd_ctl_open(struct inode *inode, struct file *file)
 
 static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
 {
+       unsigned long flags;
        struct snd_kctl_event *cread;
        
-       spin_lock(&ctl->read_lock);
+       spin_lock_irqsave(&ctl->read_lock, flags);
        while (!list_empty(&ctl->events)) {
                cread = snd_kctl_event(ctl->events.next);
                list_del(&cread->list);
                kfree(cread);
        }
-       spin_unlock(&ctl->read_lock);
+       spin_unlock_irqrestore(&ctl->read_lock, flags);
 }
 
 static int snd_ctl_release(struct inode *inode, struct file *file)
 {
        unsigned long flags;
-       struct list_head *list;
        struct snd_card *card;
        struct snd_ctl_file *ctl;
        struct snd_kcontrol *control;
@@ -122,12 +120,10 @@ static int snd_ctl_release(struct inode *inode, struct file *file)
        list_del(&ctl->list);
        write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
        down_write(&card->controls_rwsem);
-       list_for_each(list, &card->controls) {
-               control = snd_kcontrol(list);
+       list_for_each_entry(control, &card->controls, list)
                for (idx = 0; idx < control->count; idx++)
                        if (control->vd[idx].owner == ctl)
                                control->vd[idx].owner = NULL;
-       }
        up_write(&card->controls_rwsem);
        snd_ctl_empty_read_queue(ctl);
        kfree(ctl);
@@ -140,7 +136,6 @@ void snd_ctl_notify(struct snd_card *card, unsigned int mask,
                    struct snd_ctl_elem_id *id)
 {
        unsigned long flags;
-       struct list_head *flist;
        struct snd_ctl_file *ctl;
        struct snd_kctl_event *ev;
        
@@ -149,14 +144,11 @@ void snd_ctl_notify(struct snd_card *card, unsigned int mask,
 #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
        card->mixer_oss_change_count++;
 #endif
-       list_for_each(flist, &card->ctl_files) {
-               struct list_head *elist;
-               ctl = snd_ctl_file(flist);
+       list_for_each_entry(ctl, &card->ctl_files, list) {
                if (!ctl->subscribed)
                        continue;
                spin_lock_irqsave(&ctl->read_lock, flags);
-               list_for_each(elist, &ctl->events) {
-                       ev = snd_kctl_event(elist);
+               list_for_each_entry(ev, &ctl->events, list) {
                        if (ev->id.numid == id->numid) {
                                ev->mask |= mask;
                                goto _found;
@@ -190,7 +182,8 @@ EXPORT_SYMBOL(snd_ctl_notify);
  *
  * Returns the pointer of the new instance, or NULL on failure.
  */
-struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int access)
+static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
+                                       unsigned int access)
 {
        struct snd_kcontrol *kctl;
        unsigned int idx;
@@ -208,8 +201,6 @@ struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control, unsigned int acce
        return kctl;
 }
 
-EXPORT_SYMBOL(snd_ctl_new);
-
 /**
  * snd_ctl_new1 - create a control instance from the template
  * @ncontrol: the initialization record
@@ -240,8 +231,6 @@ struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
        access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
                 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
                                      SNDRV_CTL_ELEM_ACCESS_INACTIVE|
-                                     SNDRV_CTL_ELEM_ACCESS_DINDIRECT|
-                                     SNDRV_CTL_ELEM_ACCESS_INDIRECT|
                                      SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
                                      SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
        kctl.info = ncontrol->info;
@@ -277,11 +266,9 @@ EXPORT_SYMBOL(snd_ctl_free_one);
 static unsigned int snd_ctl_hole_check(struct snd_card *card,
                                       unsigned int count)
 {
-       struct list_head *list;
        struct snd_kcontrol *kctl;
 
-       list_for_each(list, &card->controls) {
-               kctl = snd_kcontrol(list);
+       list_for_each_entry(kctl, &card->controls, list) {
                if ((kctl->id.numid <= card->last_numid &&
                     kctl->id.numid + kctl->count > card->last_numid) ||
                    (kctl->id.numid <= card->last_numid + count - 1 &&
@@ -498,12 +485,10 @@ EXPORT_SYMBOL(snd_ctl_rename_id);
  */
 struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
 {
-       struct list_head *list;
        struct snd_kcontrol *kctl;
 
        snd_assert(card != NULL && numid != 0, return NULL);
-       list_for_each(list, &card->controls) {
-               kctl = snd_kcontrol(list);
+       list_for_each_entry(kctl, &card->controls, list) {
                if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
                        return kctl;
        }
@@ -527,14 +512,12 @@ EXPORT_SYMBOL(snd_ctl_find_numid);
 struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
                                     struct snd_ctl_elem_id *id)
 {
-       struct list_head *list;
        struct snd_kcontrol *kctl;
 
        snd_assert(card != NULL && id != NULL, return NULL);
        if (id->numid != 0)
                return snd_ctl_find_numid(card, id->numid);
-       list_for_each(list, &card->controls) {
-               kctl = snd_kcontrol(list);
+       list_for_each_entry(kctl, &card->controls, list) {
                if (kctl->id.iface != id->iface)
                        continue;
                if (kctl->id.device != id->device)
@@ -706,7 +689,7 @@ int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
        struct snd_kcontrol *kctl;
        struct snd_kcontrol_volatile *vd;
        unsigned int index_offset;
-       int result, indirect;
+       int result;
 
        down_read(&card->controls_rwsem);
        kctl = snd_ctl_find_id(card, &control->id);
@@ -715,24 +698,17 @@ int snd_ctl_elem_read(struct snd_card *card, struct snd_ctl_elem_value *control)
        } else {
                index_offset = snd_ctl_get_ioff(kctl, &control->id);
                vd = &kctl->vd[index_offset];
-               indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
-               if (control->indirect != indirect) {
-                       result = -EACCES;
-               } else {
-                       if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) && kctl->get != NULL) {
-                               snd_ctl_build_ioff(&control->id, kctl, index_offset);
-                               result = kctl->get(kctl, control);
-                       } else {
-                               result = -EPERM;
-                       }
-               }
+               if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
+                   kctl->get != NULL) {
+                       snd_ctl_build_ioff(&control->id, kctl, index_offset);
+                       result = kctl->get(kctl, control);
+               } else
+                       result = -EPERM;
        }
        up_read(&card->controls_rwsem);
        return result;
 }
 
-EXPORT_SYMBOL(snd_ctl_elem_read);
-
 static int snd_ctl_elem_read_user(struct snd_card *card,
                                  struct snd_ctl_elem_value __user *_control)
 {
@@ -764,7 +740,7 @@ int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
        struct snd_kcontrol *kctl;
        struct snd_kcontrol_volatile *vd;
        unsigned int index_offset;
-       int result, indirect;
+       int result;
 
        down_read(&card->controls_rwsem);
        kctl = snd_ctl_find_id(card, &control->id);
@@ -773,31 +749,25 @@ int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
        } else {
                index_offset = snd_ctl_get_ioff(kctl, &control->id);
                vd = &kctl->vd[index_offset];
-               indirect = vd->access & SNDRV_CTL_ELEM_ACCESS_INDIRECT ? 1 : 0;
-               if (control->indirect != indirect) {
-                       result = -EACCES;
+               if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
+                   kctl->put == NULL ||
+                   (file && vd->owner && vd->owner != file)) {
+                       result = -EPERM;
                } else {
-                       if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
-                           kctl->put == NULL ||
-                           (file && vd->owner != NULL && vd->owner != file)) {
-                               result = -EPERM;
-                       } else {
-                               snd_ctl_build_ioff(&control->id, kctl, index_offset);
-                               result = kctl->put(kctl, control);
-                       }
-                       if (result > 0) {
-                               up_read(&card->controls_rwsem);
-                               snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &control->id);
-                               return 0;
-                       }
+                       snd_ctl_build_ioff(&control->id, kctl, index_offset);
+                       result = kctl->put(kctl, control);
+               }
+               if (result > 0) {
+                       up_read(&card->controls_rwsem);
+                       snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
+                                      &control->id);
+                       return 0;
                }
        }
        up_read(&card->controls_rwsem);
        return result;
 }
 
-EXPORT_SYMBOL(snd_ctl_elem_write);
-
 static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
                                   struct snd_ctl_elem_value __user *_control)
 {
@@ -1182,7 +1152,6 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 {
        struct snd_ctl_file *ctl;
        struct snd_card *card;
-       struct list_head *list;
        struct snd_kctl_ioctl *p;
        void __user *argp = (void __user *)arg;
        int __user *ip = argp;
@@ -1232,8 +1201,7 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
 #endif
        }
        down_read(&snd_ioctl_rwsem);
-       list_for_each(list, &snd_control_ioctls) {
-               p = list_entry(list, struct snd_kctl_ioctl, list);
+       list_for_each_entry(p, &snd_control_ioctls, list) {
                err = p->fioctl(card, ctl, cmd, arg);
                if (err != -ENOIOCTLCMD) {
                        up_read(&snd_ioctl_rwsem);
@@ -1357,13 +1325,11 @@ EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
 static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
                                     struct list_head *lists)
 {
-       struct list_head *list;
        struct snd_kctl_ioctl *p;
 
        snd_assert(fcn != NULL, return -EINVAL);
        down_write(&snd_ioctl_rwsem);
-       list_for_each(list, lists) {
-               p = list_entry(list, struct snd_kctl_ioctl, list);
+       list_for_each_entry(p, lists, list) {
                if (p->fioctl == fcn) {
                        list_del(&p->list);
                        up_write(&snd_ioctl_rwsem);
@@ -1416,7 +1382,7 @@ static int snd_ctl_fasync(int fd, struct file * file, int on)
  *  INIT PART
  */
 
-static struct file_operations snd_ctl_f_ops =
+static const struct file_operations snd_ctl_f_ops =
 {
        .owner =        THIS_MODULE,
        .read =         snd_ctl_read,
@@ -1453,7 +1419,6 @@ static int snd_ctl_dev_register(struct snd_device *device)
 static int snd_ctl_dev_disconnect(struct snd_device *device)
 {
        struct snd_card *card = device->device_data;
-       struct list_head *flist;
        struct snd_ctl_file *ctl;
        int err, cardnum;
 
@@ -1462,8 +1427,7 @@ static int snd_ctl_dev_disconnect(struct snd_device *device)
        snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
 
        down_read(&card->controls_rwsem);
-       list_for_each(flist, &card->ctl_files) {
-               ctl = snd_ctl_file(flist);
+       list_for_each_entry(ctl, &card->ctl_files, list) {
                wake_up(&ctl->change_sleep);
                kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
        }
@@ -1507,3 +1471,30 @@ int snd_ctl_create(struct snd_card *card)
        snd_assert(card != NULL, return -ENXIO);
        return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
 }
+
+/*
+ * Frequently used control callbacks
+ */
+int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
+                             struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+       uinfo->count = 1;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 1;
+       return 0;
+}
+
+EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
+
+int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
+                               struct snd_ctl_elem_info *uinfo)
+{
+       uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
+       uinfo->count = 2;
+       uinfo->value.integer.min = 0;
+       uinfo->value.integer.max = 1;
+       return 0;
+}
+
+EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);