2 * Virtual master and slave controls
4 * Copyright (c) 2008 by Takashi Iwai <tiwai@suse.de>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
12 #include <linux/slab.h>
13 #include <sound/core.h>
14 #include <sound/control.h>
15 #include <sound/tlv.h>
18 * a subset of information returned via ctl info callback
20 struct link_ctl_info {
21 int type; /* value type */
22 int count; /* item count */
23 int min_val, max_val; /* min, max values */
27 * link master - this contains a list of slave controls that are
28 * identical types, i.e. info returns the same value type and value
29 * ranges, but may have different number of counts.
31 * The master control is so far only mono volume/switch for simplicity.
32 * The same value will be applied to all slaves.
35 struct list_head slaves;
36 struct link_ctl_info info;
37 int val; /* the master value */
42 * link slave - this contains a slave control element
44 * It fakes the control callbacsk with additional attenuation by the
45 * master control. A slave may have either one or two channels.
49 struct list_head list;
50 struct link_master *master;
51 struct link_ctl_info info;
52 int vals[2]; /* current values */
53 struct snd_kcontrol slave; /* the copy of original control entry */
56 /* get the slave ctl info and save the initial values */
57 static int slave_init(struct link_slave *slave)
59 struct snd_ctl_elem_info *uinfo;
60 struct snd_ctl_elem_value *uctl;
63 if (slave->info.count)
64 return 0; /* already initialized */
66 uinfo = kmalloc(sizeof(*uinfo), GFP_KERNEL);
69 uinfo->id = slave->slave.id;
70 err = slave->slave.info(&slave->slave, uinfo);
75 slave->info.type = uinfo->type;
76 slave->info.count = uinfo->count;
77 if (slave->info.count > 2 ||
78 (slave->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER &&
79 slave->info.type != SNDRV_CTL_ELEM_TYPE_BOOLEAN)) {
80 snd_printk(KERN_ERR "invalid slave element\n");
84 slave->info.min_val = uinfo->value.integer.min;
85 slave->info.max_val = uinfo->value.integer.max;
88 uctl = kmalloc(sizeof(*uctl), GFP_KERNEL);
91 uctl->id = slave->slave.id;
92 err = slave->slave.get(&slave->slave, uctl);
93 for (ch = 0; ch < slave->info.count; ch++)
94 slave->vals[ch] = uctl->value.integer.value[ch];
99 /* initialize master volume */
100 static int master_init(struct link_master *master)
102 struct link_slave *slave;
104 if (master->info.count)
105 return 0; /* already initialized */
107 list_for_each_entry(slave, &master->slaves, list) {
108 int err = slave_init(slave);
111 master->info = slave->info;
112 master->info.count = 1; /* always mono */
113 /* set full volume as default (= no attenuation) */
114 master->val = master->info.max_val;
120 static int slave_get_val(struct link_slave *slave,
121 struct snd_ctl_elem_value *ucontrol)
125 err = slave_init(slave);
128 for (ch = 0; ch < slave->info.count; ch++)
129 ucontrol->value.integer.value[ch] = slave->vals[ch];
133 static int slave_put_val(struct link_slave *slave,
134 struct snd_ctl_elem_value *ucontrol)
138 err = master_init(slave->master);
142 switch (slave->info.type) {
143 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
144 for (ch = 0; ch < slave->info.count; ch++)
145 ucontrol->value.integer.value[ch] &=
146 !!slave->master->val;
148 case SNDRV_CTL_ELEM_TYPE_INTEGER:
149 for (ch = 0; ch < slave->info.count; ch++) {
150 /* max master volume is supposed to be 0 dB */
151 vol = ucontrol->value.integer.value[ch];
152 vol += slave->master->val - slave->master->info.max_val;
153 if (vol < slave->info.min_val)
154 vol = slave->info.min_val;
155 else if (vol > slave->info.max_val)
156 vol = slave->info.max_val;
157 ucontrol->value.integer.value[ch] = vol;
161 return slave->slave.put(&slave->slave, ucontrol);
165 * ctl callbacks for slaves
167 static int slave_info(struct snd_kcontrol *kcontrol,
168 struct snd_ctl_elem_info *uinfo)
170 struct link_slave *slave = snd_kcontrol_chip(kcontrol);
171 return slave->slave.info(&slave->slave, uinfo);
174 static int slave_get(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
177 struct link_slave *slave = snd_kcontrol_chip(kcontrol);
178 return slave_get_val(slave, ucontrol);
181 static int slave_put(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
184 struct link_slave *slave = snd_kcontrol_chip(kcontrol);
185 int err, ch, changed = 0;
187 err = slave_init(slave);
190 for (ch = 0; ch < slave->info.count; ch++) {
191 if (slave->vals[ch] != ucontrol->value.integer.value[ch]) {
193 slave->vals[ch] = ucontrol->value.integer.value[ch];
198 return slave_put_val(slave, ucontrol);
201 static int slave_tlv_cmd(struct snd_kcontrol *kcontrol,
202 int op_flag, unsigned int size,
203 unsigned int __user *tlv)
205 struct link_slave *slave = snd_kcontrol_chip(kcontrol);
206 /* FIXME: this assumes that the max volume is 0 dB */
207 return slave->slave.tlv.c(&slave->slave, op_flag, size, tlv);
210 static void slave_free(struct snd_kcontrol *kcontrol)
212 struct link_slave *slave = snd_kcontrol_chip(kcontrol);
213 if (slave->slave.private_free)
214 slave->slave.private_free(&slave->slave);
216 list_del(&slave->list);
221 * Add a slave control to the group with the given master control
223 * All slaves must be the same type (returning the same information
224 * via info callback). The fucntion doesn't check it, so it's your
227 * Also, some additional limitations:
228 * - at most two channels
229 * - logarithmic volume control (dB level), no linear volume
230 * - master can only attenuate the volume, no gain
232 int snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave)
234 struct link_master *master_link = snd_kcontrol_chip(master);
235 struct link_slave *srec;
237 srec = kzalloc(sizeof(*srec) +
238 slave->count * sizeof(*slave->vd), GFP_KERNEL);
241 srec->slave = *slave;
242 memcpy(srec->slave.vd, slave->vd, slave->count * sizeof(*slave->vd));
243 srec->master = master_link;
245 /* override callbacks */
246 slave->info = slave_info;
247 slave->get = slave_get;
248 slave->put = slave_put;
249 if (slave->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)
250 slave->tlv.c = slave_tlv_cmd;
251 slave->private_data = srec;
252 slave->private_free = slave_free;
254 list_add_tail(&srec->list, &master_link->slaves);
258 EXPORT_SYMBOL(snd_ctl_add_slave);
261 * ctl callbacks for master controls
263 static int master_info(struct snd_kcontrol *kcontrol,
264 struct snd_ctl_elem_info *uinfo)
266 struct link_master *master = snd_kcontrol_chip(kcontrol);
269 ret = master_init(master);
272 uinfo->type = master->info.type;
273 uinfo->count = master->info.count;
274 uinfo->value.integer.min = master->info.min_val;
275 uinfo->value.integer.max = master->info.max_val;
279 static int master_get(struct snd_kcontrol *kcontrol,
280 struct snd_ctl_elem_value *ucontrol)
282 struct link_master *master = snd_kcontrol_chip(kcontrol);
283 int err = master_init(master);
286 ucontrol->value.integer.value[0] = master->val;
290 static int master_put(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
293 struct link_master *master = snd_kcontrol_chip(kcontrol);
294 struct link_slave *slave;
295 struct snd_ctl_elem_value *uval;
298 err = master_init(master);
301 old_val = master->val;
302 if (ucontrol->value.integer.value[0] == old_val)
305 uval = kmalloc(sizeof(*uval), GFP_KERNEL);
308 list_for_each_entry(slave, &master->slaves, list) {
309 master->val = old_val;
310 uval->id = slave->slave.id;
311 slave_get_val(slave, uval);
312 master->val = ucontrol->value.integer.value[0];
313 slave_put_val(slave, uval);
319 static void master_free(struct snd_kcontrol *kcontrol)
321 struct link_master *master = snd_kcontrol_chip(kcontrol);
322 struct link_slave *slave;
324 list_for_each_entry(slave, &master->slaves, list)
325 slave->master = NULL;
331 * Create a virtual master control with the given name
333 struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
334 const unsigned int *tlv)
336 struct link_master *master;
337 struct snd_kcontrol *kctl;
338 struct snd_kcontrol_new knew;
340 memset(&knew, 0, sizeof(knew));
341 knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
343 knew.info = master_info;
345 master = kzalloc(sizeof(*master), GFP_KERNEL);
348 INIT_LIST_HEAD(&master->slaves);
350 kctl = snd_ctl_new1(&knew, master);
355 /* override some callbacks */
356 kctl->info = master_info;
357 kctl->get = master_get;
358 kctl->put = master_put;
359 kctl->private_free = master_free;
361 /* additional (constant) TLV read */
362 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
363 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
364 memcpy(master->tlv, tlv, sizeof(master->tlv));
365 kctl->tlv.p = master->tlv;
371 EXPORT_SYMBOL(snd_ctl_make_virtual_master);