0001
0002
0003
0004
0005
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
0016
0017 struct link_ctl_info {
0018 snd_ctl_elem_type_t type;
0019 int count;
0020 int min_val, max_val;
0021 };
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 struct link_master {
0032 struct list_head followers;
0033 struct link_ctl_info info;
0034 int val;
0035 unsigned int tlv[4];
0036 void (*hook)(void *private_data, int);
0037 void *hook_private_data;
0038 };
0039
0040
0041
0042
0043
0044
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];
0052 unsigned int flags;
0053 struct snd_kcontrol *kctl;
0054 struct snd_kcontrol follower;
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
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
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
0115 static int master_init(struct link_master *master)
0116 {
0117 struct link_follower *follower;
0118
0119 if (master->info.count)
0120 return 0;
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;
0128
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
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
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
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
0242
0243
0244
0245
0246
0247
0248
0249
0250
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
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
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
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;
0369 kfree(follower);
0370 }
0371 kfree(master);
0372 }
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
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
0415 kctl->info = master_info;
0416 kctl->get = master_get;
0417 kctl->put = master_put;
0418 kctl->private_free = master_free;
0419
0420
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
0438
0439
0440
0441
0442
0443
0444
0445
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
0460
0461
0462
0463
0464
0465
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
0492
0493
0494
0495
0496
0497
0498
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);