Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Virtual master and follower controls
0004  *
0005  *  Copyright (c) 2008 by Takashi Iwai <tiwai@suse.de>
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/export.h>
0010 #include <sound/core.h>
0011 #include <sound/control.h>
0012 #include <sound/tlv.h>
0013 
0014 /*
0015  * a subset of information returned via ctl info callback
0016  */
0017 struct link_ctl_info {
0018     snd_ctl_elem_type_t type; /* value type */
0019     int count;      /* item count */
0020     int min_val, max_val;   /* min, max values */
0021 };
0022 
0023 /*
0024  * link master - this contains a list of follower controls that are
0025  * identical types, i.e. info returns the same value type and value
0026  * ranges, but may have different number of counts.
0027  *
0028  * The master control is so far only mono volume/switch for simplicity.
0029  * The same value will be applied to all followers.
0030  */
0031 struct link_master {
0032     struct list_head followers;
0033     struct link_ctl_info info;
0034     int val;        /* the master value */
0035     unsigned int tlv[4];
0036     void (*hook)(void *private_data, int);
0037     void *hook_private_data;
0038 };
0039 
0040 /*
0041  * link follower - this contains a follower control element
0042  *
0043  * It fakes the control callbacks with additional attenuation by the
0044  * master control.  A follower may have either one or two channels.
0045  */
0046 
0047 struct link_follower {
0048     struct list_head list;
0049     struct link_master *master;
0050     struct link_ctl_info info;
0051     int vals[2];        /* current values */
0052     unsigned int flags;
0053     struct snd_kcontrol *kctl; /* original kcontrol pointer */
0054     struct snd_kcontrol follower; /* the copy of original control entry */
0055 };
0056 
0057 static int follower_update(struct link_follower *follower)
0058 {
0059     struct snd_ctl_elem_value *uctl;
0060     int err, ch;
0061 
0062     uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
0063     if (!uctl)
0064         return -ENOMEM;
0065     uctl->id = follower->follower.id;
0066     err = follower->follower.get(&follower->follower, uctl);
0067     if (err < 0)
0068         goto error;
0069     for (ch = 0; ch < follower->info.count; ch++)
0070         follower->vals[ch] = uctl->value.integer.value[ch];
0071  error:
0072     kfree(uctl);
0073     return err < 0 ? err : 0;
0074 }
0075 
0076 /* get the follower ctl info and save the initial values */
0077 static int follower_init(struct link_follower *follower)
0078 {
0079     struct snd_ctl_elem_info *uinfo;
0080     int err;
0081 
0082     if (follower->info.count) {
0083         /* already initialized */
0084         if (follower->flags & SND_CTL_FOLLOWER_NEED_UPDATE)
0085             return follower_update(follower);
0086         return 0;
0087     }
0088 
0089     uinfo = kmalloc(sizeof(*uinfo), GFP_KERNEL);
0090     if (!uinfo)
0091         return -ENOMEM;
0092     uinfo->id = follower->follower.id;
0093     err = follower->follower.info(&follower->follower, uinfo);
0094     if (err < 0) {
0095         kfree(uinfo);
0096         return err;
0097     }
0098     follower->info.type = uinfo->type;
0099     follower->info.count = uinfo->count;
0100     if (follower->info.count > 2  ||
0101         (follower->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER &&
0102          follower->info.type != SNDRV_CTL_ELEM_TYPE_BOOLEAN)) {
0103         pr_err("ALSA: vmaster: invalid follower element\n");
0104         kfree(uinfo);
0105         return -EINVAL;
0106     }
0107     follower->info.min_val = uinfo->value.integer.min;
0108     follower->info.max_val = uinfo->value.integer.max;
0109     kfree(uinfo);
0110 
0111     return follower_update(follower);
0112 }
0113 
0114 /* initialize master volume */
0115 static int master_init(struct link_master *master)
0116 {
0117     struct link_follower *follower;
0118 
0119     if (master->info.count)
0120         return 0; /* already initialized */
0121 
0122     list_for_each_entry(follower, &master->followers, list) {
0123         int err = follower_init(follower);
0124         if (err < 0)
0125             return err;
0126         master->info = follower->info;
0127         master->info.count = 1; /* always mono */
0128         /* set full volume as default (= no attenuation) */
0129         master->val = master->info.max_val;
0130         if (master->hook)
0131             master->hook(master->hook_private_data, master->val);
0132         return 1;
0133     }
0134     return -ENOENT;
0135 }
0136 
0137 static int follower_get_val(struct link_follower *follower,
0138                 struct snd_ctl_elem_value *ucontrol)
0139 {
0140     int err, ch;
0141 
0142     err = follower_init(follower);
0143     if (err < 0)
0144         return err;
0145     for (ch = 0; ch < follower->info.count; ch++)
0146         ucontrol->value.integer.value[ch] = follower->vals[ch];
0147     return 0;
0148 }
0149 
0150 static int follower_put_val(struct link_follower *follower,
0151                 struct snd_ctl_elem_value *ucontrol)
0152 {
0153     int err, ch, vol;
0154 
0155     err = master_init(follower->master);
0156     if (err < 0)
0157         return err;
0158 
0159     switch (follower->info.type) {
0160     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
0161         for (ch = 0; ch < follower->info.count; ch++)
0162             ucontrol->value.integer.value[ch] &=
0163                 !!follower->master->val;
0164         break;
0165     case SNDRV_CTL_ELEM_TYPE_INTEGER:
0166         for (ch = 0; ch < follower->info.count; ch++) {
0167             /* max master volume is supposed to be 0 dB */
0168             vol = ucontrol->value.integer.value[ch];
0169             vol += follower->master->val - follower->master->info.max_val;
0170             if (vol < follower->info.min_val)
0171                 vol = follower->info.min_val;
0172             else if (vol > follower->info.max_val)
0173                 vol = follower->info.max_val;
0174             ucontrol->value.integer.value[ch] = vol;
0175         }
0176         break;
0177     }
0178     return follower->follower.put(&follower->follower, ucontrol);
0179 }
0180 
0181 /*
0182  * ctl callbacks for followers
0183  */
0184 static int follower_info(struct snd_kcontrol *kcontrol,
0185              struct snd_ctl_elem_info *uinfo)
0186 {
0187     struct link_follower *follower = snd_kcontrol_chip(kcontrol);
0188     return follower->follower.info(&follower->follower, uinfo);
0189 }
0190 
0191 static int follower_get(struct snd_kcontrol *kcontrol,
0192             struct snd_ctl_elem_value *ucontrol)
0193 {
0194     struct link_follower *follower = snd_kcontrol_chip(kcontrol);
0195     return follower_get_val(follower, ucontrol);
0196 }
0197 
0198 static int follower_put(struct snd_kcontrol *kcontrol,
0199             struct snd_ctl_elem_value *ucontrol)
0200 {
0201     struct link_follower *follower = snd_kcontrol_chip(kcontrol);
0202     int err, ch, changed = 0;
0203 
0204     err = follower_init(follower);
0205     if (err < 0)
0206         return err;
0207     for (ch = 0; ch < follower->info.count; ch++) {
0208         if (follower->vals[ch] != ucontrol->value.integer.value[ch]) {
0209             changed = 1;
0210             follower->vals[ch] = ucontrol->value.integer.value[ch];
0211         }
0212     }
0213     if (!changed)
0214         return 0;
0215     err = follower_put_val(follower, ucontrol);
0216     if (err < 0)
0217         return err;
0218     return 1;
0219 }
0220 
0221 static int follower_tlv_cmd(struct snd_kcontrol *kcontrol,
0222                 int op_flag, unsigned int size,
0223                 unsigned int __user *tlv)
0224 {
0225     struct link_follower *follower = snd_kcontrol_chip(kcontrol);
0226     /* FIXME: this assumes that the max volume is 0 dB */
0227     return follower->follower.tlv.c(&follower->follower, op_flag, size, tlv);
0228 }
0229 
0230 static void follower_free(struct snd_kcontrol *kcontrol)
0231 {
0232     struct link_follower *follower = snd_kcontrol_chip(kcontrol);
0233     if (follower->follower.private_free)
0234         follower->follower.private_free(&follower->follower);
0235     if (follower->master)
0236         list_del(&follower->list);
0237     kfree(follower);
0238 }
0239 
0240 /*
0241  * Add a follower control to the group with the given master control
0242  *
0243  * All followers must be the same type (returning the same information
0244  * via info callback).  The function doesn't check it, so it's your
0245  * responsibility.
0246  *
0247  * Also, some additional limitations:
0248  * - at most two channels
0249  * - logarithmic volume control (dB level), no linear volume
0250  * - master can only attenuate the volume, no gain
0251  */
0252 int _snd_ctl_add_follower(struct snd_kcontrol *master,
0253               struct snd_kcontrol *follower,
0254               unsigned int flags)
0255 {
0256     struct link_master *master_link = snd_kcontrol_chip(master);
0257     struct link_follower *srec;
0258 
0259     srec = kzalloc(struct_size(srec, follower.vd, follower->count),
0260                GFP_KERNEL);
0261     if (!srec)
0262         return -ENOMEM;
0263     srec->kctl = follower;
0264     srec->follower = *follower;
0265     memcpy(srec->follower.vd, follower->vd, follower->count * sizeof(*follower->vd));
0266     srec->master = master_link;
0267     srec->flags = flags;
0268 
0269     /* override callbacks */
0270     follower->info = follower_info;
0271     follower->get = follower_get;
0272     follower->put = follower_put;
0273     if (follower->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)
0274         follower->tlv.c = follower_tlv_cmd;
0275     follower->private_data = srec;
0276     follower->private_free = follower_free;
0277 
0278     list_add_tail(&srec->list, &master_link->followers);
0279     return 0;
0280 }
0281 EXPORT_SYMBOL(_snd_ctl_add_follower);
0282 
0283 /*
0284  * ctl callbacks for master controls
0285  */
0286 static int master_info(struct snd_kcontrol *kcontrol,
0287               struct snd_ctl_elem_info *uinfo)
0288 {
0289     struct link_master *master = snd_kcontrol_chip(kcontrol);
0290     int ret;
0291 
0292     ret = master_init(master);
0293     if (ret < 0)
0294         return ret;
0295     uinfo->type = master->info.type;
0296     uinfo->count = master->info.count;
0297     uinfo->value.integer.min = master->info.min_val;
0298     uinfo->value.integer.max = master->info.max_val;
0299     return 0;
0300 }
0301 
0302 static int master_get(struct snd_kcontrol *kcontrol,
0303               struct snd_ctl_elem_value *ucontrol)
0304 {
0305     struct link_master *master = snd_kcontrol_chip(kcontrol);
0306     int err = master_init(master);
0307     if (err < 0)
0308         return err;
0309     ucontrol->value.integer.value[0] = master->val;
0310     return 0;
0311 }
0312 
0313 static int sync_followers(struct link_master *master, int old_val, int new_val)
0314 {
0315     struct link_follower *follower;
0316     struct snd_ctl_elem_value *uval;
0317 
0318     uval = kmalloc(sizeof(*uval), GFP_KERNEL);
0319     if (!uval)
0320         return -ENOMEM;
0321     list_for_each_entry(follower, &master->followers, list) {
0322         master->val = old_val;
0323         uval->id = follower->follower.id;
0324         follower_get_val(follower, uval);
0325         master->val = new_val;
0326         follower_put_val(follower, uval);
0327     }
0328     kfree(uval);
0329     return 0;
0330 }
0331 
0332 static int master_put(struct snd_kcontrol *kcontrol,
0333               struct snd_ctl_elem_value *ucontrol)
0334 {
0335     struct link_master *master = snd_kcontrol_chip(kcontrol);
0336     int err, new_val, old_val;
0337     bool first_init;
0338 
0339     err = master_init(master);
0340     if (err < 0)
0341         return err;
0342     first_init = err;
0343     old_val = master->val;
0344     new_val = ucontrol->value.integer.value[0];
0345     if (new_val == old_val)
0346         return 0;
0347 
0348     err = sync_followers(master, old_val, new_val);
0349     if (err < 0)
0350         return err;
0351     if (master->hook && !first_init)
0352         master->hook(master->hook_private_data, master->val);
0353     return 1;
0354 }
0355 
0356 static void master_free(struct snd_kcontrol *kcontrol)
0357 {
0358     struct link_master *master = snd_kcontrol_chip(kcontrol);
0359     struct link_follower *follower, *n;
0360 
0361     /* free all follower links and retore the original follower kctls */
0362     list_for_each_entry_safe(follower, n, &master->followers, list) {
0363         struct snd_kcontrol *sctl = follower->kctl;
0364         struct list_head olist = sctl->list;
0365         memcpy(sctl, &follower->follower, sizeof(*sctl));
0366         memcpy(sctl->vd, follower->follower.vd,
0367                sctl->count * sizeof(*sctl->vd));
0368         sctl->list = olist; /* keep the current linked-list */
0369         kfree(follower);
0370     }
0371     kfree(master);
0372 }
0373 
0374 
0375 /**
0376  * snd_ctl_make_virtual_master - Create a virtual master control
0377  * @name: name string of the control element to create
0378  * @tlv: optional TLV int array for dB information
0379  *
0380  * Creates a virtual master control with the given name string.
0381  *
0382  * After creating a vmaster element, you can add the follower controls
0383  * via snd_ctl_add_follower() or snd_ctl_add_follower_uncached().
0384  *
0385  * The optional argument @tlv can be used to specify the TLV information
0386  * for dB scale of the master control.  It should be a single element
0387  * with #SNDRV_CTL_TLVT_DB_SCALE, #SNDRV_CTL_TLV_DB_MINMAX or
0388  * #SNDRV_CTL_TLVT_DB_MINMAX_MUTE type, and should be the max 0dB.
0389  *
0390  * Return: The created control element, or %NULL for errors (ENOMEM).
0391  */
0392 struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
0393                          const unsigned int *tlv)
0394 {
0395     struct link_master *master;
0396     struct snd_kcontrol *kctl;
0397     struct snd_kcontrol_new knew;
0398 
0399     memset(&knew, 0, sizeof(knew));
0400     knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0401     knew.name = name;
0402     knew.info = master_info;
0403 
0404     master = kzalloc(sizeof(*master), GFP_KERNEL);
0405     if (!master)
0406         return NULL;
0407     INIT_LIST_HEAD(&master->followers);
0408 
0409     kctl = snd_ctl_new1(&knew, master);
0410     if (!kctl) {
0411         kfree(master);
0412         return NULL;
0413     }
0414     /* override some callbacks */
0415     kctl->info = master_info;
0416     kctl->get = master_get;
0417     kctl->put = master_put;
0418     kctl->private_free = master_free;
0419 
0420     /* additional (constant) TLV read */
0421     if (tlv) {
0422         unsigned int type = tlv[SNDRV_CTL_TLVO_TYPE];
0423         if (type == SNDRV_CTL_TLVT_DB_SCALE ||
0424             type == SNDRV_CTL_TLVT_DB_MINMAX ||
0425             type == SNDRV_CTL_TLVT_DB_MINMAX_MUTE) {
0426             kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
0427             memcpy(master->tlv, tlv, sizeof(master->tlv));
0428             kctl->tlv.p = master->tlv;
0429         }
0430     }
0431 
0432     return kctl;
0433 }
0434 EXPORT_SYMBOL(snd_ctl_make_virtual_master);
0435 
0436 /**
0437  * snd_ctl_add_vmaster_hook - Add a hook to a vmaster control
0438  * @kcontrol: vmaster kctl element
0439  * @hook: the hook function
0440  * @private_data: the private_data pointer to be saved
0441  *
0442  * Adds the given hook to the vmaster control element so that it's called
0443  * at each time when the value is changed.
0444  *
0445  * Return: Zero.
0446  */
0447 int snd_ctl_add_vmaster_hook(struct snd_kcontrol *kcontrol,
0448                  void (*hook)(void *private_data, int),
0449                  void *private_data)
0450 {
0451     struct link_master *master = snd_kcontrol_chip(kcontrol);
0452     master->hook = hook;
0453     master->hook_private_data = private_data;
0454     return 0;
0455 }
0456 EXPORT_SYMBOL_GPL(snd_ctl_add_vmaster_hook);
0457 
0458 /**
0459  * snd_ctl_sync_vmaster - Sync the vmaster followers and hook
0460  * @kcontrol: vmaster kctl element
0461  * @hook_only: sync only the hook
0462  *
0463  * Forcibly call the put callback of each follower and call the hook function
0464  * to synchronize with the current value of the given vmaster element.
0465  * NOP when NULL is passed to @kcontrol.
0466  */
0467 void snd_ctl_sync_vmaster(struct snd_kcontrol *kcontrol, bool hook_only)
0468 {
0469     struct link_master *master;
0470     bool first_init = false;
0471 
0472     if (!kcontrol)
0473         return;
0474     master = snd_kcontrol_chip(kcontrol);
0475     if (!hook_only) {
0476         int err = master_init(master);
0477         if (err < 0)
0478             return;
0479         first_init = err;
0480         err = sync_followers(master, master->val, master->val);
0481         if (err < 0)
0482             return;
0483     }
0484 
0485     if (master->hook && !first_init)
0486         master->hook(master->hook_private_data, master->val);
0487 }
0488 EXPORT_SYMBOL_GPL(snd_ctl_sync_vmaster);
0489 
0490 /**
0491  * snd_ctl_apply_vmaster_followers - Apply function to each vmaster follower
0492  * @kctl: vmaster kctl element
0493  * @func: function to apply
0494  * @arg: optional function argument
0495  *
0496  * Apply the function @func to each follower kctl of the given vmaster kctl.
0497  *
0498  * Return: 0 if successful, or a negative error code
0499  */
0500 int snd_ctl_apply_vmaster_followers(struct snd_kcontrol *kctl,
0501                     int (*func)(struct snd_kcontrol *vfollower,
0502                         struct snd_kcontrol *follower,
0503                         void *arg),
0504                     void *arg)
0505 {
0506     struct link_master *master;
0507     struct link_follower *follower;
0508     int err;
0509 
0510     master = snd_kcontrol_chip(kctl);
0511     err = master_init(master);
0512     if (err < 0)
0513         return err;
0514     list_for_each_entry(follower, &master->followers, list) {
0515         err = func(follower->kctl, &follower->follower, arg);
0516         if (err < 0)
0517             return err;
0518     }
0519 
0520     return 0;
0521 }
0522 EXPORT_SYMBOL_GPL(snd_ctl_apply_vmaster_followers);