ALSA: seq: Fix race at timer setup and close
[pandora-kernel.git] / sound / core / vmaster.c
1 /*
2  * Virtual master and slave controls
3  *
4  *  Copyright (c) 2008 by Takashi Iwai <tiwai@suse.de>
5  *
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.
9  *
10  */
11
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include <sound/tlv.h>
17
18 /*
19  * a subset of information returned via ctl info callback
20  */
21 struct link_ctl_info {
22         snd_ctl_elem_type_t type; /* value type */
23         int count;              /* item count */
24         int min_val, max_val;   /* min, max values */
25 };
26
27 /*
28  * link master - this contains a list of slave controls that are
29  * identical types, i.e. info returns the same value type and value
30  * ranges, but may have different number of counts.
31  *
32  * The master control is so far only mono volume/switch for simplicity.
33  * The same value will be applied to all slaves.
34  */
35 struct link_master {
36         struct list_head slaves;
37         struct link_ctl_info info;
38         int val;                /* the master value */
39         unsigned int tlv[4];
40 };
41
42 /*
43  * link slave - this contains a slave control element
44  *
45  * It fakes the control callbacsk with additional attenuation by the
46  * master control.  A slave may have either one or two channels.
47  */
48
49 struct link_slave {
50         struct list_head list;
51         struct link_master *master;
52         struct link_ctl_info info;
53         int vals[2];            /* current values */
54         unsigned int flags;
55         struct snd_kcontrol *kctl; /* original kcontrol pointer */
56         struct snd_kcontrol slave; /* the copy of original control entry */
57 };
58
59 static int slave_update(struct link_slave *slave)
60 {
61         struct snd_ctl_elem_value *uctl;
62         int err, ch;
63
64         uctl = kmalloc(sizeof(*uctl), GFP_KERNEL);
65         if (!uctl)
66                 return -ENOMEM;
67         uctl->id = slave->slave.id;
68         err = slave->slave.get(&slave->slave, uctl);
69         for (ch = 0; ch < slave->info.count; ch++)
70                 slave->vals[ch] = uctl->value.integer.value[ch];
71         kfree(uctl);
72         return 0;
73 }
74
75 /* get the slave ctl info and save the initial values */
76 static int slave_init(struct link_slave *slave)
77 {
78         struct snd_ctl_elem_info *uinfo;
79         int err;
80
81         if (slave->info.count) {
82                 /* already initialized */
83                 if (slave->flags & SND_CTL_SLAVE_NEED_UPDATE)
84                         return slave_update(slave);
85                 return 0;
86         }
87
88         uinfo = kmalloc(sizeof(*uinfo), GFP_KERNEL);
89         if (!uinfo)
90                 return -ENOMEM;
91         uinfo->id = slave->slave.id;
92         err = slave->slave.info(&slave->slave, uinfo);
93         if (err < 0) {
94                 kfree(uinfo);
95                 return err;
96         }
97         slave->info.type = uinfo->type;
98         slave->info.count = uinfo->count;
99         if (slave->info.count > 2  ||
100             (slave->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER &&
101              slave->info.type != SNDRV_CTL_ELEM_TYPE_BOOLEAN)) {
102                 snd_printk(KERN_ERR "invalid slave element\n");
103                 kfree(uinfo);
104                 return -EINVAL;
105         }
106         slave->info.min_val = uinfo->value.integer.min;
107         slave->info.max_val = uinfo->value.integer.max;
108         kfree(uinfo);
109
110         return slave_update(slave);
111 }
112
113 /* initialize master volume */
114 static int master_init(struct link_master *master)
115 {
116         struct link_slave *slave;
117
118         if (master->info.count)
119                 return 0; /* already initialized */
120
121         list_for_each_entry(slave, &master->slaves, list) {
122                 int err = slave_init(slave);
123                 if (err < 0)
124                         return err;
125                 master->info = slave->info;
126                 master->info.count = 1; /* always mono */
127                 /* set full volume as default (= no attenuation) */
128                 master->val = master->info.max_val;
129                 return 0;
130         }
131         return -ENOENT;
132 }
133
134 static int slave_get_val(struct link_slave *slave,
135                          struct snd_ctl_elem_value *ucontrol)
136 {
137         int err, ch;
138
139         err = slave_init(slave);
140         if (err < 0)
141                 return err;
142         for (ch = 0; ch < slave->info.count; ch++)
143                 ucontrol->value.integer.value[ch] = slave->vals[ch];
144         return 0;
145 }
146
147 static int slave_put_val(struct link_slave *slave,
148                          struct snd_ctl_elem_value *ucontrol)
149 {
150         int err, ch, vol;
151
152         err = master_init(slave->master);
153         if (err < 0)
154                 return err;
155
156         switch (slave->info.type) {
157         case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
158                 for (ch = 0; ch < slave->info.count; ch++)
159                         ucontrol->value.integer.value[ch] &=
160                                 !!slave->master->val;
161                 break;
162         case SNDRV_CTL_ELEM_TYPE_INTEGER:
163                 for (ch = 0; ch < slave->info.count; ch++) {
164                         /* max master volume is supposed to be 0 dB */
165                         vol = ucontrol->value.integer.value[ch];
166                         vol += slave->master->val - slave->master->info.max_val;
167                         if (vol < slave->info.min_val)
168                                 vol = slave->info.min_val;
169                         else if (vol > slave->info.max_val)
170                                 vol = slave->info.max_val;
171                         ucontrol->value.integer.value[ch] = vol;
172                 }
173                 break;
174         }
175         return slave->slave.put(&slave->slave, ucontrol);
176 }
177
178 /*
179  * ctl callbacks for slaves
180  */
181 static int slave_info(struct snd_kcontrol *kcontrol,
182                       struct snd_ctl_elem_info *uinfo)
183 {
184         struct link_slave *slave = snd_kcontrol_chip(kcontrol);
185         return slave->slave.info(&slave->slave, uinfo);
186 }
187
188 static int slave_get(struct snd_kcontrol *kcontrol,
189                      struct snd_ctl_elem_value *ucontrol)
190 {
191         struct link_slave *slave = snd_kcontrol_chip(kcontrol);
192         return slave_get_val(slave, ucontrol);
193 }
194
195 static int slave_put(struct snd_kcontrol *kcontrol,
196                      struct snd_ctl_elem_value *ucontrol)
197 {
198         struct link_slave *slave = snd_kcontrol_chip(kcontrol);
199         int err, ch, changed = 0;
200
201         err = slave_init(slave);
202         if (err < 0)
203                 return err;
204         for (ch = 0; ch < slave->info.count; ch++) {
205                 if (slave->vals[ch] != ucontrol->value.integer.value[ch]) {
206                         changed = 1;
207                         slave->vals[ch] = ucontrol->value.integer.value[ch];
208                 }
209         }
210         if (!changed)
211                 return 0;
212         err = slave_put_val(slave, ucontrol);
213         if (err < 0)
214                 return err;
215         return 1;
216 }
217
218 static int slave_tlv_cmd(struct snd_kcontrol *kcontrol,
219                          int op_flag, unsigned int size,
220                          unsigned int __user *tlv)
221 {
222         struct link_slave *slave = snd_kcontrol_chip(kcontrol);
223         /* FIXME: this assumes that the max volume is 0 dB */
224         return slave->slave.tlv.c(&slave->slave, op_flag, size, tlv);
225 }
226
227 static void slave_free(struct snd_kcontrol *kcontrol)
228 {
229         struct link_slave *slave = snd_kcontrol_chip(kcontrol);
230         if (slave->slave.private_free)
231                 slave->slave.private_free(&slave->slave);
232         if (slave->master)
233                 list_del(&slave->list);
234         kfree(slave);
235 }
236
237 /*
238  * Add a slave control to the group with the given master control
239  *
240  * All slaves must be the same type (returning the same information
241  * via info callback).  The function doesn't check it, so it's your
242  * responsibility.
243  *
244  * Also, some additional limitations:
245  * - at most two channels
246  * - logarithmic volume control (dB level), no linear volume
247  * - master can only attenuate the volume, no gain
248  */
249 int _snd_ctl_add_slave(struct snd_kcontrol *master, struct snd_kcontrol *slave,
250                        unsigned int flags)
251 {
252         struct link_master *master_link = snd_kcontrol_chip(master);
253         struct link_slave *srec;
254
255         srec = kzalloc(sizeof(*srec) +
256                        slave->count * sizeof(*slave->vd), GFP_KERNEL);
257         if (!srec)
258                 return -ENOMEM;
259         srec->kctl = slave;
260         srec->slave = *slave;
261         memcpy(srec->slave.vd, slave->vd, slave->count * sizeof(*slave->vd));
262         srec->master = master_link;
263         srec->flags = flags;
264
265         /* override callbacks */
266         slave->info = slave_info;
267         slave->get = slave_get;
268         slave->put = slave_put;
269         if (slave->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)
270                 slave->tlv.c = slave_tlv_cmd;
271         slave->private_data = srec;
272         slave->private_free = slave_free;
273
274         list_add_tail(&srec->list, &master_link->slaves);
275         return 0;
276 }
277 EXPORT_SYMBOL(_snd_ctl_add_slave);
278
279 /*
280  * ctl callbacks for master controls
281  */
282 static int master_info(struct snd_kcontrol *kcontrol,
283                       struct snd_ctl_elem_info *uinfo)
284 {
285         struct link_master *master = snd_kcontrol_chip(kcontrol);
286         int ret;
287
288         ret = master_init(master);
289         if (ret < 0)
290                 return ret;
291         uinfo->type = master->info.type;
292         uinfo->count = master->info.count;
293         uinfo->value.integer.min = master->info.min_val;
294         uinfo->value.integer.max = master->info.max_val;
295         return 0;
296 }
297
298 static int master_get(struct snd_kcontrol *kcontrol,
299                       struct snd_ctl_elem_value *ucontrol)
300 {
301         struct link_master *master = snd_kcontrol_chip(kcontrol);
302         int err = master_init(master);
303         if (err < 0)
304                 return err;
305         ucontrol->value.integer.value[0] = master->val;
306         return 0;
307 }
308
309 static int master_put(struct snd_kcontrol *kcontrol,
310                       struct snd_ctl_elem_value *ucontrol)
311 {
312         struct link_master *master = snd_kcontrol_chip(kcontrol);
313         struct link_slave *slave;
314         struct snd_ctl_elem_value *uval;
315         int err, old_val;
316
317         err = master_init(master);
318         if (err < 0)
319                 return err;
320         old_val = master->val;
321         if (ucontrol->value.integer.value[0] == old_val)
322                 return 0;
323
324         uval = kmalloc(sizeof(*uval), GFP_KERNEL);
325         if (!uval)
326                 return -ENOMEM;
327         list_for_each_entry(slave, &master->slaves, list) {
328                 master->val = old_val;
329                 uval->id = slave->slave.id;
330                 slave_get_val(slave, uval);
331                 master->val = ucontrol->value.integer.value[0];
332                 slave_put_val(slave, uval);
333         }
334         kfree(uval);
335         return 1;
336 }
337
338 static void master_free(struct snd_kcontrol *kcontrol)
339 {
340         struct link_master *master = snd_kcontrol_chip(kcontrol);
341         struct link_slave *slave, *n;
342
343         /* free all slave links and retore the original slave kctls */
344         list_for_each_entry_safe(slave, n, &master->slaves, list) {
345                 struct snd_kcontrol *sctl = slave->kctl;
346                 struct list_head olist = sctl->list;
347                 memcpy(sctl, &slave->slave, sizeof(*sctl));
348                 memcpy(sctl->vd, slave->slave.vd,
349                        sctl->count * sizeof(*sctl->vd));
350                 sctl->list = olist; /* keep the current linked-list */
351                 kfree(slave);
352         }
353         kfree(master);
354 }
355
356
357 /**
358  * snd_ctl_make_virtual_master - Create a virtual master control
359  * @name: name string of the control element to create
360  * @tlv: optional TLV int array for dB information
361  *
362  * Creates a virtual matster control with the given name string.
363  * Returns the created control element, or NULL for errors (ENOMEM).
364  *
365  * After creating a vmaster element, you can add the slave controls
366  * via snd_ctl_add_slave() or snd_ctl_add_slave_uncached().
367  *
368  * The optional argument @tlv can be used to specify the TLV information
369  * for dB scale of the master control.  It should be a single element
370  * with #SNDRV_CTL_TLVT_DB_SCALE, #SNDRV_CTL_TLV_DB_MINMAX or
371  * #SNDRV_CTL_TLVT_DB_MINMAX_MUTE type, and should be the max 0dB.
372  */
373 struct snd_kcontrol *snd_ctl_make_virtual_master(char *name,
374                                                  const unsigned int *tlv)
375 {
376         struct link_master *master;
377         struct snd_kcontrol *kctl;
378         struct snd_kcontrol_new knew;
379
380         memset(&knew, 0, sizeof(knew));
381         knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
382         knew.name = name;
383         knew.info = master_info;
384
385         master = kzalloc(sizeof(*master), GFP_KERNEL);
386         if (!master)
387                 return NULL;
388         INIT_LIST_HEAD(&master->slaves);
389
390         kctl = snd_ctl_new1(&knew, master);
391         if (!kctl) {
392                 kfree(master);
393                 return NULL;
394         }
395         /* override some callbacks */
396         kctl->info = master_info;
397         kctl->get = master_get;
398         kctl->put = master_put;
399         kctl->private_free = master_free;
400
401         /* additional (constant) TLV read */
402         if (tlv &&
403             (tlv[0] == SNDRV_CTL_TLVT_DB_SCALE ||
404              tlv[0] == SNDRV_CTL_TLVT_DB_MINMAX ||
405              tlv[0] == SNDRV_CTL_TLVT_DB_MINMAX_MUTE)) {
406                 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
407                 memcpy(master->tlv, tlv, sizeof(master->tlv));
408                 kctl->tlv.p = master->tlv;
409         }
410
411         return kctl;
412 }
413 EXPORT_SYMBOL(snd_ctl_make_virtual_master);