blob: 5c49f976fc7b3c93351b2b417e919002802c8e4c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for driver control interface
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/threads.h>
23#include <linux/interrupt.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/slab.h>
26#include <linux/vmalloc.h>
27#include <linux/time.h>
28#include <sound/core.h>
29#include <sound/minors.h>
30#include <sound/info.h>
31#include <sound/control.h>
32
33/* max number of user-defined controls */
34#define MAX_USER_CONTROLS 32
Dan Rosenberg5591bf02010-09-28 14:18:20 -040035#define MAX_CONTROL_COUNT 1028
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Takashi Iwai82e9bae2005-11-17 13:53:23 +010037struct snd_kctl_ioctl {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct list_head list; /* list of all ioctls */
39 snd_kctl_ioctl_func_t fioctl;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010040};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static DECLARE_RWSEM(snd_ioctl_rwsem);
43static LIST_HEAD(snd_control_ioctls);
44#ifdef CONFIG_COMPAT
45static LIST_HEAD(snd_control_compat_ioctls);
46#endif
47
48static int snd_ctl_open(struct inode *inode, struct file *file)
49{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +010051 struct snd_card *card;
52 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int err;
54
Takashi Iwai02f48652010-04-13 11:49:04 +020055 err = nonseekable_open(inode, file);
56 if (err < 0)
57 return err;
58
Clemens Ladischf87135f2005-11-20 14:06:59 +010059 card = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!card) {
61 err = -ENODEV;
62 goto __error1;
63 }
64 err = snd_card_file_add(card, file);
65 if (err < 0) {
66 err = -ENODEV;
67 goto __error1;
68 }
69 if (!try_module_get(card->module)) {
70 err = -EFAULT;
71 goto __error2;
72 }
Takashi Iwaica2c0962005-09-09 14:20:23 +020073 ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (ctl == NULL) {
75 err = -ENOMEM;
76 goto __error;
77 }
78 INIT_LIST_HEAD(&ctl->events);
79 init_waitqueue_head(&ctl->change_sleep);
80 spin_lock_init(&ctl->read_lock);
81 ctl->card = card;
Takashi Iwai2529bba2006-08-04 12:57:19 +020082 ctl->prefer_pcm_subdevice = -1;
83 ctl->prefer_rawmidi_subdevice = -1;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +010084 ctl->pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 file->private_data = ctl;
86 write_lock_irqsave(&card->ctl_files_rwlock, flags);
87 list_add_tail(&ctl->list, &card->ctl_files);
88 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
Takashi Iwaia0830db2012-10-16 13:05:59 +020089 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return 0;
91
92 __error:
93 module_put(card->module);
94 __error2:
95 snd_card_file_remove(card, file);
96 __error1:
Takashi Iwaia0830db2012-10-16 13:05:59 +020097 if (card)
98 snd_card_unref(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return err;
100}
101
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100102static void snd_ctl_empty_read_queue(struct snd_ctl_file * ctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200104 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100105 struct snd_kctl_event *cread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200107 spin_lock_irqsave(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 while (!list_empty(&ctl->events)) {
109 cread = snd_kctl_event(ctl->events.next);
110 list_del(&cread->list);
111 kfree(cread);
112 }
Borislav Petkov7507e8d2007-10-22 17:11:09 +0200113 spin_unlock_irqrestore(&ctl->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static int snd_ctl_release(struct inode *inode, struct file *file)
117{
118 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100119 struct snd_card *card;
120 struct snd_ctl_file *ctl;
121 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int idx;
123
124 ctl = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 file->private_data = NULL;
126 card = ctl->card;
127 write_lock_irqsave(&card->ctl_files_rwlock, flags);
128 list_del(&ctl->list);
129 write_unlock_irqrestore(&card->ctl_files_rwlock, flags);
130 down_write(&card->controls_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200131 list_for_each_entry(control, &card->controls, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 for (idx = 0; idx < control->count; idx++)
133 if (control->vd[idx].owner == ctl)
134 control->vd[idx].owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 up_write(&card->controls_rwsem);
136 snd_ctl_empty_read_queue(ctl);
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100137 put_pid(ctl->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 kfree(ctl);
139 module_put(card->module);
140 snd_card_file_remove(card, file);
141 return 0;
142}
143
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100144void snd_ctl_notify(struct snd_card *card, unsigned int mask,
145 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 unsigned long flags;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100148 struct snd_ctl_file *ctl;
149 struct snd_kctl_event *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200151 if (snd_BUG_ON(!card || !id))
152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 read_lock(&card->ctl_files_rwlock);
Takashi Iwai8eeaa2f2014-02-10 09:48:47 +0100154#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 card->mixer_oss_change_count++;
156#endif
Johannes Berg9244b2c2006-10-05 16:02:22 +0200157 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!ctl->subscribed)
159 continue;
160 spin_lock_irqsave(&ctl->read_lock, flags);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200161 list_for_each_entry(ev, &ctl->events, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (ev->id.numid == id->numid) {
163 ev->mask |= mask;
164 goto _found;
165 }
166 }
Takashi Iwaica2c0962005-09-09 14:20:23 +0200167 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (ev) {
169 ev->id = *id;
170 ev->mask = mask;
171 list_add_tail(&ev->list, &ctl->events);
172 } else {
Takashi Iwaibb009452014-02-04 18:18:16 +0100173 dev_err(card->dev, "No memory available to allocate event\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175 _found:
176 wake_up(&ctl->change_sleep);
177 spin_unlock_irqrestore(&ctl->read_lock, flags);
178 kill_fasync(&ctl->fasync, SIGIO, POLL_IN);
179 }
180 read_unlock(&card->ctl_files_rwlock);
181}
182
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200183EXPORT_SYMBOL(snd_ctl_notify);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/**
186 * snd_ctl_new - create a control instance from the template
187 * @control: the control template
188 * @access: the default control access
189 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100190 * Allocates a new struct snd_kcontrol instance and copies the given template
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * to the new instance. It does not copy volatile data (access).
192 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100193 * Return: The pointer of the new instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Adrian Bunk0b51ba02006-11-20 17:50:17 +0100195static struct snd_kcontrol *snd_ctl_new(struct snd_kcontrol *control,
196 unsigned int access)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100198 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned int idx;
200
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200201 if (snd_BUG_ON(!control || !control->count))
202 return NULL;
Dan Rosenberg5591bf02010-09-28 14:18:20 -0400203
204 if (control->count > MAX_CONTROL_COUNT)
205 return NULL;
206
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100207 kctl = kzalloc(sizeof(*kctl) + sizeof(struct snd_kcontrol_volatile) * control->count, GFP_KERNEL);
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100208 if (kctl == NULL) {
Takashi Iwaibb009452014-02-04 18:18:16 +0100209 pr_err("ALSA: Cannot allocate control instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return NULL;
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *kctl = *control;
213 for (idx = 0; idx < kctl->count; idx++)
214 kctl->vd[idx].access = access;
215 return kctl;
216}
217
218/**
219 * snd_ctl_new1 - create a control instance from the template
220 * @ncontrol: the initialization record
221 * @private_data: the private data to set
222 *
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100223 * Allocates a new struct snd_kcontrol instance and initialize from the given
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * template. When the access field of ncontrol is 0, it's assumed as
225 * READWRITE access. When the count field is 0, it's assumes as one.
226 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100227 * Return: The pointer of the newly generated instance, or %NULL on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100229struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
230 void *private_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100232 struct snd_kcontrol kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned int access;
234
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200235 if (snd_BUG_ON(!ncontrol || !ncontrol->info))
236 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 memset(&kctl, 0, sizeof(kctl));
238 kctl.id.iface = ncontrol->iface;
239 kctl.id.device = ncontrol->device;
240 kctl.id.subdevice = ncontrol->subdevice;
Mark Brown366840d2008-10-29 14:40:30 +0000241 if (ncontrol->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
Mark Brown366840d2008-10-29 14:40:30 +0000243 if (strcmp(ncontrol->name, kctl.id.name) != 0)
Takashi Iwaibb009452014-02-04 18:18:16 +0100244 pr_warn("ALSA: Control name '%s' truncated to '%s'\n",
245 ncontrol->name, kctl.id.name);
Mark Brown366840d2008-10-29 14:40:30 +0000246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 kctl.id.index = ncontrol->index;
248 kctl.count = ncontrol->count ? ncontrol->count : 1;
249 access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200250 (ncontrol->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Takashi Iwaia8d372f2012-07-30 13:47:07 +0200251 SNDRV_CTL_ELEM_ACCESS_VOLATILE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200252 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
Clemens Ladischa75d7a42010-02-01 13:29:50 +0100253 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE|
254 SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND|
255 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 kctl.info = ncontrol->info;
257 kctl.get = ncontrol->get;
258 kctl.put = ncontrol->put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +0200259 kctl.tlv.p = ncontrol->tlv.p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 kctl.private_value = ncontrol->private_value;
261 kctl.private_data = private_data;
262 return snd_ctl_new(&kctl, access);
263}
264
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200265EXPORT_SYMBOL(snd_ctl_new1);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
268 * snd_ctl_free_one - release the control instance
269 * @kcontrol: the control instance
270 *
271 * Releases the control instance created via snd_ctl_new()
272 * or snd_ctl_new1().
273 * Don't call this after the control was added to the card.
274 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100275void snd_ctl_free_one(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 if (kcontrol) {
278 if (kcontrol->private_free)
279 kcontrol->private_free(kcontrol);
280 kfree(kcontrol);
281 }
282}
283
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200284EXPORT_SYMBOL(snd_ctl_free_one);
285
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100286static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
287 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100289 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Johannes Berg9244b2c2006-10-05 16:02:22 +0200291 list_for_each_entry(kctl, &card->controls, list) {
Clemens Ladisch7c733582011-03-07 13:22:50 +0100292 if (kctl->id.numid < card->last_numid + 1 + count &&
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100293 kctl->id.numid + kctl->count > card->last_numid + 1) {
294 card->last_numid = kctl->id.numid + kctl->count - 1;
295 return true;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100298 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100301static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100303 unsigned int iter = 100000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Clemens Ladisch0e82e5f2011-03-07 13:24:30 +0100305 while (snd_ctl_remove_numid_conflict(card, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (--iter == 0) {
307 /* this situation is very unlikely */
Takashi Iwaibb009452014-02-04 18:18:16 +0100308 dev_err(card->dev, "unable to allocate new control numid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -ENOMEM;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 return 0;
313}
314
315/**
316 * snd_ctl_add - add the control instance to the card
317 * @card: the card instance
318 * @kcontrol: the control instance to add
319 *
320 * Adds the control instance created via snd_ctl_new() or
321 * snd_ctl_new1() to the given card. Assigns also an unique
322 * numid used for fast search.
323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * It frees automatically the control which cannot be added.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100325 *
326 * Return: Zero if successful, or a negative error code on failure.
327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100329int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100331 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned int idx;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200333 unsigned int count;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100334 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Takashi Iwai73e77ba2005-11-17 17:44:01 +0100336 if (! kcontrol)
Takashi Iwaic6077b32006-03-21 16:07:13 +0100337 return err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200338 if (snd_BUG_ON(!card || !kcontrol->info))
339 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 id = kcontrol->id;
341 down_write(&card->controls_rwsem);
342 if (snd_ctl_find_id(card, &id)) {
343 up_write(&card->controls_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +0100344 dev_err(card->dev, "control %i:%i:%i:%s:%i is already present\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 id.iface,
346 id.device,
347 id.subdevice,
348 id.name,
349 id.index);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100350 err = -EBUSY;
351 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
354 up_write(&card->controls_rwsem);
Takashi Iwaic6077b32006-03-21 16:07:13 +0100355 err = -ENOMEM;
356 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 list_add_tail(&kcontrol->list, &card->controls);
359 card->controls_count += kcontrol->count;
360 kcontrol->id.numid = card->last_numid + 1;
361 card->last_numid += kcontrol->count;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200362 count = kcontrol->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200364 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
366 return 0;
Takashi Iwaic6077b32006-03-21 16:07:13 +0100367
368 error:
369 snd_ctl_free_one(kcontrol);
370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200373EXPORT_SYMBOL(snd_ctl_add);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000376 * snd_ctl_replace - replace the control instance of the card
377 * @card: the card instance
378 * @kcontrol: the control instance to replace
379 * @add_on_replace: add the control if not already added
380 *
381 * Replaces the given control. If the given control does not exist
382 * and the add_on_replace flag is set, the control is added. If the
383 * control exists, it is destroyed first.
384 *
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000385 * It frees automatically the control which cannot be added or replaced.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100386 *
387 * Return: Zero if successful, or a negative error code on failure.
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000388 */
389int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
390 bool add_on_replace)
391{
392 struct snd_ctl_elem_id id;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200393 unsigned int count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000394 unsigned int idx;
395 struct snd_kcontrol *old;
396 int ret;
397
398 if (!kcontrol)
399 return -EINVAL;
400 if (snd_BUG_ON(!card || !kcontrol->info)) {
401 ret = -EINVAL;
402 goto error;
403 }
404 id = kcontrol->id;
405 down_write(&card->controls_rwsem);
406 old = snd_ctl_find_id(card, &id);
407 if (!old) {
408 if (add_on_replace)
409 goto add;
410 up_write(&card->controls_rwsem);
411 ret = -EINVAL;
412 goto error;
413 }
414 ret = snd_ctl_remove(card, old);
415 if (ret < 0) {
416 up_write(&card->controls_rwsem);
417 goto error;
418 }
419add:
420 if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
421 up_write(&card->controls_rwsem);
422 ret = -ENOMEM;
423 goto error;
424 }
425 list_add_tail(&kcontrol->list, &card->controls);
426 card->controls_count += kcontrol->count;
427 kcontrol->id.numid = card->last_numid + 1;
428 card->last_numid += kcontrol->count;
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200429 count = kcontrol->count;
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000430 up_write(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200431 for (idx = 0; idx < count; idx++, id.index++, id.numid++)
Dimitris Papastamos66b5b972011-03-16 12:16:39 +0000432 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
433 return 0;
434
435error:
436 snd_ctl_free_one(kcontrol);
437 return ret;
438}
439EXPORT_SYMBOL(snd_ctl_replace);
440
441/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * snd_ctl_remove - remove the control from the card and release it
443 * @card: the card instance
444 * @kcontrol: the control instance to remove
445 *
446 * Removes the control from the card and then releases the instance.
447 * You don't need to call snd_ctl_free_one(). You must be in
448 * the write lock - down_write(&card->controls_rwsem).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100449 *
450 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100452int snd_ctl_remove(struct snd_card *card, struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100454 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned int idx;
456
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200457 if (snd_BUG_ON(!card || !kcontrol))
458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 list_del(&kcontrol->list);
460 card->controls_count -= kcontrol->count;
461 id = kcontrol->id;
462 for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
463 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_REMOVE, &id);
464 snd_ctl_free_one(kcontrol);
465 return 0;
466}
467
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200468EXPORT_SYMBOL(snd_ctl_remove);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/**
471 * snd_ctl_remove_id - remove the control of the given id and release it
472 * @card: the card instance
473 * @id: the control id to remove
474 *
475 * Finds the control instance with the given id, removes it from the
476 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100477 *
478 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100480int snd_ctl_remove_id(struct snd_card *card, struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100482 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int ret;
484
485 down_write(&card->controls_rwsem);
486 kctl = snd_ctl_find_id(card, id);
487 if (kctl == NULL) {
488 up_write(&card->controls_rwsem);
489 return -ENOENT;
490 }
491 ret = snd_ctl_remove(card, kctl);
492 up_write(&card->controls_rwsem);
493 return ret;
494}
495
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200496EXPORT_SYMBOL(snd_ctl_remove_id);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/**
Clemens Ladischf217ac52009-08-17 12:27:22 +0200499 * snd_ctl_remove_user_ctl - remove and release the unlocked user control
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * @file: active control handle
501 * @id: the control id to remove
502 *
503 * Finds the control instance with the given id, removes it from the
504 * card list and releases it.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100505 *
506 * Return: 0 if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Clemens Ladischf217ac52009-08-17 12:27:22 +0200508static int snd_ctl_remove_user_ctl(struct snd_ctl_file * file,
509 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100511 struct snd_card *card = file->card;
512 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int idx, ret;
514
515 down_write(&card->controls_rwsem);
516 kctl = snd_ctl_find_id(card, id);
517 if (kctl == NULL) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200518 ret = -ENOENT;
519 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Clemens Ladisch18dd0aa2009-08-17 12:28:09 +0200521 if (!(kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER)) {
522 ret = -EINVAL;
523 goto error;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 for (idx = 0; idx < kctl->count; idx++)
526 if (kctl->vd[idx].owner != NULL && kctl->vd[idx].owner != file) {
Clemens Ladisch317b8082009-08-17 12:26:34 +0200527 ret = -EBUSY;
528 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 ret = snd_ctl_remove(card, kctl);
Clemens Ladischf217ac52009-08-17 12:27:22 +0200531 if (ret < 0)
532 goto error;
533 card->user_ctl_count--;
Clemens Ladisch317b8082009-08-17 12:26:34 +0200534error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 up_write(&card->controls_rwsem);
536 return ret;
537}
538
539/**
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200540 * snd_ctl_activate_id - activate/inactivate the control of the given id
541 * @card: the card instance
542 * @id: the control id to activate/inactivate
543 * @active: non-zero to activate
544 *
545 * Finds the control instance with the given id, and activate or
546 * inactivate the control together with notification, if changed.
547 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100548 * Return: 0 if unchanged, 1 if changed, or a negative error code on failure.
Takashi Iwai3cbdd752008-08-29 16:09:01 +0200549 */
550int snd_ctl_activate_id(struct snd_card *card, struct snd_ctl_elem_id *id,
551 int active)
552{
553 struct snd_kcontrol *kctl;
554 struct snd_kcontrol_volatile *vd;
555 unsigned int index_offset;
556 int ret;
557
558 down_write(&card->controls_rwsem);
559 kctl = snd_ctl_find_id(card, id);
560 if (kctl == NULL) {
561 ret = -ENOENT;
562 goto unlock;
563 }
564 index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
565 vd = &kctl->vd[index_offset];
566 ret = 0;
567 if (active) {
568 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
569 goto unlock;
570 vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
571 } else {
572 if (vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)
573 goto unlock;
574 vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
575 }
576 ret = 1;
577 unlock:
578 up_write(&card->controls_rwsem);
579 if (ret > 0)
580 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, id);
581 return ret;
582}
583EXPORT_SYMBOL_GPL(snd_ctl_activate_id);
584
585/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * snd_ctl_rename_id - replace the id of a control on the card
587 * @card: the card instance
588 * @src_id: the old id
589 * @dst_id: the new id
590 *
591 * Finds the control with the old id from the card, and replaces the
592 * id with the new one.
593 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100594 * Return: Zero if successful, or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100596int snd_ctl_rename_id(struct snd_card *card, struct snd_ctl_elem_id *src_id,
597 struct snd_ctl_elem_id *dst_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100599 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 down_write(&card->controls_rwsem);
602 kctl = snd_ctl_find_id(card, src_id);
603 if (kctl == NULL) {
604 up_write(&card->controls_rwsem);
605 return -ENOENT;
606 }
607 kctl->id = *dst_id;
608 kctl->id.numid = card->last_numid + 1;
609 card->last_numid += kctl->count;
610 up_write(&card->controls_rwsem);
611 return 0;
612}
613
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200614EXPORT_SYMBOL(snd_ctl_rename_id);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/**
617 * snd_ctl_find_numid - find the control instance with the given number-id
618 * @card: the card instance
619 * @numid: the number-id to search
620 *
621 * Finds the control instance with the given number-id from the card.
622 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * The caller must down card->controls_rwsem before calling this function
624 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100625 *
626 * Return: The pointer of the instance if found, or %NULL if not.
627 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100629struct snd_kcontrol *snd_ctl_find_numid(struct snd_card *card, unsigned int numid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100631 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200633 if (snd_BUG_ON(!card || !numid))
634 return NULL;
Johannes Berg9244b2c2006-10-05 16:02:22 +0200635 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (kctl->id.numid <= numid && kctl->id.numid + kctl->count > numid)
637 return kctl;
638 }
639 return NULL;
640}
641
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200642EXPORT_SYMBOL(snd_ctl_find_numid);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/**
645 * snd_ctl_find_id - find the control instance with the given id
646 * @card: the card instance
647 * @id: the id to search
648 *
649 * Finds the control instance with the given id from the card.
650 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * The caller must down card->controls_rwsem before calling this function
652 * (if the race condition can happen).
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100653 *
654 * Return: The pointer of the instance if found, or %NULL if not.
655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100657struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
658 struct snd_ctl_elem_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100660 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200662 if (snd_BUG_ON(!card || !id))
663 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (id->numid != 0)
665 return snd_ctl_find_numid(card, id->numid);
Johannes Berg9244b2c2006-10-05 16:02:22 +0200666 list_for_each_entry(kctl, &card->controls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (kctl->id.iface != id->iface)
668 continue;
669 if (kctl->id.device != id->device)
670 continue;
671 if (kctl->id.subdevice != id->subdevice)
672 continue;
673 if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
674 continue;
675 if (kctl->id.index > id->index)
676 continue;
677 if (kctl->id.index + kctl->count <= id->index)
678 continue;
679 return kctl;
680 }
681 return NULL;
682}
683
Takashi Iwaic0d3fb32006-04-28 15:13:39 +0200684EXPORT_SYMBOL(snd_ctl_find_id);
685
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100686static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned int cmd, void __user *arg)
688{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100689 struct snd_ctl_card_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Takashi Iwaica2c0962005-09-09 14:20:23 +0200691 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (! info)
693 return -ENOMEM;
694 down_read(&snd_ioctl_rwsem);
695 info->card = card->number;
696 strlcpy(info->id, card->id, sizeof(info->id));
697 strlcpy(info->driver, card->driver, sizeof(info->driver));
698 strlcpy(info->name, card->shortname, sizeof(info->name));
699 strlcpy(info->longname, card->longname, sizeof(info->longname));
700 strlcpy(info->mixername, card->mixername, sizeof(info->mixername));
701 strlcpy(info->components, card->components, sizeof(info->components));
702 up_read(&snd_ioctl_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100703 if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 kfree(info);
705 return -EFAULT;
706 }
707 kfree(info);
708 return 0;
709}
710
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100711static int snd_ctl_elem_list(struct snd_card *card,
712 struct snd_ctl_elem_list __user *_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct list_head *plist;
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100715 struct snd_ctl_elem_list list;
716 struct snd_kcontrol *kctl;
717 struct snd_ctl_elem_id *dst, *id;
Luca Tettamanti78fa2c42011-05-25 22:43:27 +0200718 unsigned int offset, space, jidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (copy_from_user(&list, _list, sizeof(list)))
721 return -EFAULT;
722 offset = list.offset;
723 space = list.space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* try limit maximum space */
725 if (space > 16384)
726 return -ENOMEM;
727 if (space > 0) {
728 /* allocate temporary buffer for atomic operation */
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100729 dst = vmalloc(space * sizeof(struct snd_ctl_elem_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (dst == NULL)
731 return -ENOMEM;
732 down_read(&card->controls_rwsem);
733 list.count = card->controls_count;
734 plist = card->controls.next;
735 while (plist != &card->controls) {
736 if (offset == 0)
737 break;
738 kctl = snd_kcontrol(plist);
739 if (offset < kctl->count)
740 break;
741 offset -= kctl->count;
742 plist = plist->next;
743 }
744 list.used = 0;
745 id = dst;
746 while (space > 0 && plist != &card->controls) {
747 kctl = snd_kcontrol(plist);
748 for (jidx = offset; space > 0 && jidx < kctl->count; jidx++) {
749 snd_ctl_build_ioff(id, kctl, jidx);
750 id++;
751 space--;
752 list.used++;
753 }
754 plist = plist->next;
755 offset = 0;
756 }
757 up_read(&card->controls_rwsem);
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100758 if (list.used > 0 &&
759 copy_to_user(list.pids, dst,
760 list.used * sizeof(struct snd_ctl_elem_id))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 vfree(dst);
762 return -EFAULT;
763 }
764 vfree(dst);
765 } else {
766 down_read(&card->controls_rwsem);
767 list.count = card->controls_count;
768 up_read(&card->controls_rwsem);
769 }
770 if (copy_to_user(_list, &list, sizeof(list)))
771 return -EFAULT;
772 return 0;
773}
774
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100775static int snd_ctl_elem_info(struct snd_ctl_file *ctl,
776 struct snd_ctl_elem_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100778 struct snd_card *card = ctl->card;
779 struct snd_kcontrol *kctl;
780 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 unsigned int index_offset;
782 int result;
783
784 down_read(&card->controls_rwsem);
785 kctl = snd_ctl_find_id(card, &info->id);
786 if (kctl == NULL) {
787 up_read(&card->controls_rwsem);
788 return -ENOENT;
789 }
790#ifdef CONFIG_SND_DEBUG
791 info->access = 0;
792#endif
793 result = kctl->info(kctl, info);
794 if (result >= 0) {
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200795 snd_BUG_ON(info->access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 index_offset = snd_ctl_get_ioff(kctl, &info->id);
797 vd = &kctl->vd[index_offset];
798 snd_ctl_build_ioff(&info->id, kctl, index_offset);
799 info->access = vd->access;
800 if (vd->owner) {
801 info->access |= SNDRV_CTL_ELEM_ACCESS_LOCK;
802 if (vd->owner == ctl)
803 info->access |= SNDRV_CTL_ELEM_ACCESS_OWNER;
Clemens Ladisch25d27ed2009-11-02 09:35:44 +0100804 info->owner = pid_vnr(vd->owner->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else {
806 info->owner = -1;
807 }
808 }
809 up_read(&card->controls_rwsem);
810 return result;
811}
812
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100813static int snd_ctl_elem_info_user(struct snd_ctl_file *ctl,
814 struct snd_ctl_elem_info __user *_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100816 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int result;
818
819 if (copy_from_user(&info, _info, sizeof(info)))
820 return -EFAULT;
Giuliano Pochini64649402006-03-13 14:11:11 +0100821 snd_power_lock(ctl->card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200822 result = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100823 if (result >= 0)
824 result = snd_ctl_elem_info(ctl, &info);
825 snd_power_unlock(ctl->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (result >= 0)
827 if (copy_to_user(_info, &info, sizeof(info)))
828 return -EFAULT;
829 return result;
830}
831
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200832static int snd_ctl_elem_read(struct snd_card *card,
833 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100835 struct snd_kcontrol *kctl;
836 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100838 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 down_read(&card->controls_rwsem);
841 kctl = snd_ctl_find_id(card, &control->id);
842 if (kctl == NULL) {
843 result = -ENOENT;
844 } else {
845 index_offset = snd_ctl_get_ioff(kctl, &control->id);
846 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100847 if ((vd->access & SNDRV_CTL_ELEM_ACCESS_READ) &&
848 kctl->get != NULL) {
849 snd_ctl_build_ioff(&control->id, kctl, index_offset);
850 result = kctl->get(kctl, control);
851 } else
852 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854 up_read(&card->controls_rwsem);
855 return result;
856}
857
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100858static int snd_ctl_elem_read_user(struct snd_card *card,
859 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100861 struct snd_ctl_elem_value *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int result;
Li Zefanef44a1e2009-04-10 09:43:08 +0800863
864 control = memdup_user(_control, sizeof(*control));
865 if (IS_ERR(control))
866 return PTR_ERR(control);
867
Giuliano Pochini64649402006-03-13 14:11:11 +0100868 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200869 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100870 if (result >= 0)
871 result = snd_ctl_elem_read(card, control);
872 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (result >= 0)
874 if (copy_to_user(_control, control, sizeof(*control)))
875 result = -EFAULT;
876 kfree(control);
877 return result;
878}
879
Takashi Iwaid3bd67c2008-06-12 18:17:26 +0200880static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
881 struct snd_ctl_elem_value *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100883 struct snd_kcontrol *kctl;
884 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned int index_offset;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100886 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 down_read(&card->controls_rwsem);
889 kctl = snd_ctl_find_id(card, &control->id);
890 if (kctl == NULL) {
891 result = -ENOENT;
892 } else {
893 index_offset = snd_ctl_get_ioff(kctl, &control->id);
894 vd = &kctl->vd[index_offset];
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100895 if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_WRITE) ||
896 kctl->put == NULL ||
897 (file && vd->owner && vd->owner != file)) {
898 result = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 } else {
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100900 snd_ctl_build_ioff(&control->id, kctl, index_offset);
901 result = kctl->put(kctl, control);
902 }
903 if (result > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200904 struct snd_ctl_elem_id id = control->id;
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100905 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +0200906 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
Takashi Iwai8ace4f32008-01-08 17:57:26 +0100907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 }
910 up_read(&card->controls_rwsem);
911 return result;
912}
913
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100914static int snd_ctl_elem_write_user(struct snd_ctl_file *file,
915 struct snd_ctl_elem_value __user *_control)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100917 struct snd_ctl_elem_value *control;
Giuliano Pochini64649402006-03-13 14:11:11 +0100918 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int result;
920
Li Zefanef44a1e2009-04-10 09:43:08 +0800921 control = memdup_user(_control, sizeof(*control));
922 if (IS_ERR(control))
923 return PTR_ERR(control);
924
Giuliano Pochini64649402006-03-13 14:11:11 +0100925 card = file->card;
926 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +0200927 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Giuliano Pochini64649402006-03-13 14:11:11 +0100928 if (result >= 0)
929 result = snd_ctl_elem_write(card, file, control);
930 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (result >= 0)
932 if (copy_to_user(_control, control, sizeof(*control)))
933 result = -EFAULT;
934 kfree(control);
935 return result;
936}
937
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100938static int snd_ctl_elem_lock(struct snd_ctl_file *file,
939 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100941 struct snd_card *card = file->card;
942 struct snd_ctl_elem_id id;
943 struct snd_kcontrol *kctl;
944 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int result;
946
947 if (copy_from_user(&id, _id, sizeof(id)))
948 return -EFAULT;
949 down_write(&card->controls_rwsem);
950 kctl = snd_ctl_find_id(card, &id);
951 if (kctl == NULL) {
952 result = -ENOENT;
953 } else {
954 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
955 if (vd->owner != NULL)
956 result = -EBUSY;
957 else {
958 vd->owner = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 result = 0;
960 }
961 }
962 up_write(&card->controls_rwsem);
963 return result;
964}
965
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100966static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
967 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100969 struct snd_card *card = file->card;
970 struct snd_ctl_elem_id id;
971 struct snd_kcontrol *kctl;
972 struct snd_kcontrol_volatile *vd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int result;
974
975 if (copy_from_user(&id, _id, sizeof(id)))
976 return -EFAULT;
977 down_write(&card->controls_rwsem);
978 kctl = snd_ctl_find_id(card, &id);
979 if (kctl == NULL) {
980 result = -ENOENT;
981 } else {
982 vd = &kctl->vd[snd_ctl_get_ioff(kctl, &id)];
983 if (vd->owner == NULL)
984 result = -EINVAL;
985 else if (vd->owner != file)
986 result = -EPERM;
987 else {
988 vd->owner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 result = 0;
990 }
991 }
992 up_write(&card->controls_rwsem);
993 return result;
994}
995
996struct user_element {
Takashi Iwai82e9bae2005-11-17 13:53:23 +0100997 struct snd_ctl_elem_info info;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +0200998 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 void *elem_data; /* element data */
1000 unsigned long elem_data_size; /* size of element data in bytes */
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001001 void *tlv_data; /* TLV data */
1002 unsigned long tlv_data_size; /* TLV data size */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 void *priv_data; /* private data (like strings for enumerated type) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004};
1005
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001006static int snd_ctl_elem_user_info(struct snd_kcontrol *kcontrol,
1007 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct user_element *ue = kcontrol->private_data;
1010
1011 *uinfo = ue->info;
1012 return 0;
1013}
1014
Clemens Ladisch8d448162011-10-07 22:38:59 +02001015static int snd_ctl_elem_user_enum_info(struct snd_kcontrol *kcontrol,
1016 struct snd_ctl_elem_info *uinfo)
1017{
1018 struct user_element *ue = kcontrol->private_data;
1019 const char *names;
1020 unsigned int item;
1021
1022 item = uinfo->value.enumerated.item;
1023
1024 *uinfo = ue->info;
1025
1026 item = min(item, uinfo->value.enumerated.items - 1);
1027 uinfo->value.enumerated.item = item;
1028
1029 names = ue->priv_data;
1030 for (; item > 0; --item)
1031 names += strlen(names) + 1;
1032 strcpy(uinfo->value.enumerated.name, names);
1033
1034 return 0;
1035}
1036
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001037static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
1038 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct user_element *ue = kcontrol->private_data;
1041
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001042 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001044 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return 0;
1046}
1047
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001048static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
1049 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
1051 int change;
1052 struct user_element *ue = kcontrol->private_data;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001053
1054 mutex_lock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
1056 if (change)
1057 memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001058 mutex_unlock(&ue->card->user_ctl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return change;
1060}
1061
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001062static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
1063 int op_flag,
1064 unsigned int size,
1065 unsigned int __user *tlv)
1066{
1067 struct user_element *ue = kcontrol->private_data;
1068 int change = 0;
1069 void *new_data;
1070
1071 if (op_flag > 0) {
1072 if (size > 1024 * 128) /* sane value */
1073 return -EINVAL;
Li Zefanef44a1e2009-04-10 09:43:08 +08001074
1075 new_data = memdup_user(tlv, size);
1076 if (IS_ERR(new_data))
1077 return PTR_ERR(new_data);
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001078 mutex_lock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001079 change = ue->tlv_data_size != size;
1080 if (!change)
1081 change = memcmp(ue->tlv_data, new_data, size);
1082 kfree(ue->tlv_data);
1083 ue->tlv_data = new_data;
1084 ue->tlv_data_size = size;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001085 mutex_unlock(&ue->card->user_ctl_lock);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001086 } else {
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001087 int ret = 0;
1088
1089 mutex_lock(&ue->card->user_ctl_lock);
1090 if (!ue->tlv_data_size || !ue->tlv_data) {
1091 ret = -ENXIO;
1092 goto err_unlock;
1093 }
1094 if (size < ue->tlv_data_size) {
1095 ret = -ENOSPC;
1096 goto err_unlock;
1097 }
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001098 if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001099 ret = -EFAULT;
1100err_unlock:
1101 mutex_unlock(&ue->card->user_ctl_lock);
1102 if (ret)
1103 return ret;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001104 }
1105 return change;
1106}
1107
Clemens Ladisch8d448162011-10-07 22:38:59 +02001108static int snd_ctl_elem_init_enum_names(struct user_element *ue)
1109{
1110 char *names, *p;
1111 size_t buf_len, name_len;
1112 unsigned int i;
Olof Johansson447c6f92011-11-05 22:51:54 +01001113 const uintptr_t user_ptrval = ue->info.value.enumerated.names_ptr;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001114
1115 if (ue->info.value.enumerated.names_length > 64 * 1024)
1116 return -EINVAL;
1117
Olof Johansson447c6f92011-11-05 22:51:54 +01001118 names = memdup_user((const void __user *)user_ptrval,
Clemens Ladisch8d448162011-10-07 22:38:59 +02001119 ue->info.value.enumerated.names_length);
1120 if (IS_ERR(names))
1121 return PTR_ERR(names);
1122
1123 /* check that there are enough valid names */
1124 buf_len = ue->info.value.enumerated.names_length;
1125 p = names;
1126 for (i = 0; i < ue->info.value.enumerated.items; ++i) {
1127 name_len = strnlen(p, buf_len);
1128 if (name_len == 0 || name_len >= 64 || name_len == buf_len) {
1129 kfree(names);
1130 return -EINVAL;
1131 }
1132 p += name_len + 1;
1133 buf_len -= name_len + 1;
1134 }
1135
1136 ue->priv_data = names;
1137 ue->info.value.enumerated.names_ptr = 0;
1138
1139 return 0;
1140}
1141
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001142static void snd_ctl_elem_user_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001144 struct user_element *ue = kcontrol->private_data;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001145
1146 kfree(ue->tlv_data);
1147 kfree(ue->priv_data);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001148 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001151static int snd_ctl_elem_add(struct snd_ctl_file *file,
1152 struct snd_ctl_elem_info *info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001154 struct snd_card *card = file->card;
1155 struct snd_kcontrol kctl, *_kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 unsigned int access;
1157 long private_size;
1158 struct user_element *ue;
1159 int idx, err;
Lu Guanqun983929c2011-08-24 11:12:34 +08001160
Clemens Ladisch2a031ae2009-08-17 12:25:52 +02001161 if (info->count < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EINVAL;
1163 access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001164 (info->access & (SNDRV_CTL_ELEM_ACCESS_READWRITE|
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001165 SNDRV_CTL_ELEM_ACCESS_INACTIVE|
1166 SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 info->id.numid = 0;
1168 memset(&kctl, 0, sizeof(kctl));
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001169
1170 if (replace) {
1171 err = snd_ctl_remove_user_ctl(file, &info->id);
1172 if (err)
1173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Lars-Peter Clausen82262a462014-06-18 13:32:32 +02001175
1176 if (card->user_ctl_count >= MAX_USER_CONTROLS)
1177 return -ENOMEM;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 memcpy(&kctl.id, &info->id, sizeof(info->id));
1180 kctl.count = info->owner ? info->owner : 1;
1181 access |= SNDRV_CTL_ELEM_ACCESS_USER;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001182 if (info->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED)
1183 kctl.info = snd_ctl_elem_user_enum_info;
1184 else
1185 kctl.info = snd_ctl_elem_user_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (access & SNDRV_CTL_ELEM_ACCESS_READ)
1187 kctl.get = snd_ctl_elem_user_get;
1188 if (access & SNDRV_CTL_ELEM_ACCESS_WRITE)
1189 kctl.put = snd_ctl_elem_user_put;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001190 if (access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
1191 kctl.tlv.c = snd_ctl_elem_user_tlv;
1192 access |= SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 switch (info->type) {
1195 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1197 private_size = sizeof(long);
1198 if (info->count > 128)
1199 return -EINVAL;
1200 break;
1201 case SNDRV_CTL_ELEM_TYPE_INTEGER64:
1202 private_size = sizeof(long long);
1203 if (info->count > 64)
1204 return -EINVAL;
1205 break;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001206 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1207 private_size = sizeof(unsigned int);
1208 if (info->count > 128 || info->value.enumerated.items == 0)
1209 return -EINVAL;
1210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 case SNDRV_CTL_ELEM_TYPE_BYTES:
1212 private_size = sizeof(unsigned char);
1213 if (info->count > 512)
1214 return -EINVAL;
1215 break;
1216 case SNDRV_CTL_ELEM_TYPE_IEC958:
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001217 private_size = sizeof(struct snd_aes_iec958);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (info->count != 1)
1219 return -EINVAL;
1220 break;
1221 default:
1222 return -EINVAL;
1223 }
1224 private_size *= info->count;
Takashi Iwaica2c0962005-09-09 14:20:23 +02001225 ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (ue == NULL)
1227 return -ENOMEM;
Lars-Peter Clausen07f4d9d2014-06-18 13:32:31 +02001228 ue->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ue->info = *info;
Takashi Iwai86148e82006-08-24 12:36:36 +02001230 ue->info.access = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 ue->elem_data = (char *)ue + sizeof(*ue);
1232 ue->elem_data_size = private_size;
Clemens Ladisch8d448162011-10-07 22:38:59 +02001233 if (ue->info.type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
1234 err = snd_ctl_elem_init_enum_names(ue);
1235 if (err < 0) {
1236 kfree(ue);
1237 return err;
1238 }
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 kctl.private_free = snd_ctl_elem_user_free;
1241 _kctl = snd_ctl_new(&kctl, access);
1242 if (_kctl == NULL) {
Clemens Ladisch8d448162011-10-07 22:38:59 +02001243 kfree(ue->priv_data);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001244 kfree(ue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -ENOMEM;
1246 }
1247 _kctl->private_data = ue;
1248 for (idx = 0; idx < _kctl->count; idx++)
1249 _kctl->vd[idx].owner = file;
1250 err = snd_ctl_add(card, _kctl);
Takashi Iwai2fbf1822006-03-06 15:42:51 -08001251 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 down_write(&card->controls_rwsem);
1255 card->user_ctl_count++;
1256 up_write(&card->controls_rwsem);
1257
1258 return 0;
1259}
1260
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001261static int snd_ctl_elem_add_user(struct snd_ctl_file *file,
1262 struct snd_ctl_elem_info __user *_info, int replace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001264 struct snd_ctl_elem_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (copy_from_user(&info, _info, sizeof(info)))
1266 return -EFAULT;
1267 return snd_ctl_elem_add(file, &info, replace);
1268}
1269
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001270static int snd_ctl_elem_remove(struct snd_ctl_file *file,
1271 struct snd_ctl_elem_id __user *_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001273 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (copy_from_user(&id, _id, sizeof(id)))
1276 return -EFAULT;
Clemens Ladischf217ac52009-08-17 12:27:22 +02001277 return snd_ctl_remove_user_ctl(file, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001280static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 int subscribe;
1283 if (get_user(subscribe, ptr))
1284 return -EFAULT;
1285 if (subscribe < 0) {
1286 subscribe = file->subscribed;
1287 if (put_user(subscribe, ptr))
1288 return -EFAULT;
1289 return 0;
1290 }
1291 if (subscribe) {
1292 file->subscribed = 1;
1293 return 0;
1294 } else if (file->subscribed) {
1295 snd_ctl_empty_read_queue(file);
1296 file->subscribed = 0;
1297 }
1298 return 0;
1299}
1300
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001301static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
1302 struct snd_ctl_tlv __user *_tlv,
1303 int op_flag)
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001304{
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001305 struct snd_card *card = file->card;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001306 struct snd_ctl_tlv tlv;
1307 struct snd_kcontrol *kctl;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001308 struct snd_kcontrol_volatile *vd;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001309 unsigned int len;
1310 int err = 0;
1311
1312 if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1313 return -EFAULT;
Clemens Ladisch61236372010-02-01 13:30:56 +01001314 if (tlv.length < sizeof(unsigned int) * 2)
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001315 return -EINVAL;
1316 down_read(&card->controls_rwsem);
1317 kctl = snd_ctl_find_numid(card, tlv.numid);
1318 if (kctl == NULL) {
1319 err = -ENOENT;
1320 goto __kctl_end;
1321 }
1322 if (kctl->tlv.p == NULL) {
1323 err = -ENXIO;
1324 goto __kctl_end;
1325 }
1326 vd = &kctl->vd[tlv.numid - kctl->id.numid];
1327 if ((op_flag == 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) == 0) ||
1328 (op_flag > 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) == 0) ||
1329 (op_flag < 0 && (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND) == 0)) {
1330 err = -ENXIO;
1331 goto __kctl_end;
1332 }
1333 if (vd->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
Dan Carpenterbec145a2009-11-18 10:31:57 +02001334 if (vd->owner != NULL && vd->owner != file) {
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001335 err = -EPERM;
1336 goto __kctl_end;
1337 }
Jeffrin Josebd483d42012-03-07 22:57:39 +05301338 err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001339 if (err > 0) {
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001340 struct snd_ctl_elem_id id = kctl->id;
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001341 up_read(&card->controls_rwsem);
Lars-Peter Clausenfd9f26e2014-06-18 13:32:33 +02001342 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001343 return 0;
1344 }
1345 } else {
1346 if (op_flag) {
1347 err = -ENXIO;
1348 goto __kctl_end;
1349 }
1350 len = kctl->tlv.p[1] + 2 * sizeof(unsigned int);
1351 if (tlv.length < len) {
1352 err = -ENOMEM;
1353 goto __kctl_end;
1354 }
1355 if (copy_to_user(_tlv->tlv, kctl->tlv.p, len))
1356 err = -EFAULT;
1357 }
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001358 __kctl_end:
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001359 up_read(&card->controls_rwsem);
1360 return err;
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001361}
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1364{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001365 struct snd_ctl_file *ctl;
1366 struct snd_card *card;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001367 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 void __user *argp = (void __user *)arg;
1369 int __user *ip = argp;
1370 int err;
1371
1372 ctl = file->private_data;
1373 card = ctl->card;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001374 if (snd_BUG_ON(!card))
1375 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 switch (cmd) {
1377 case SNDRV_CTL_IOCTL_PVERSION:
1378 return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
1379 case SNDRV_CTL_IOCTL_CARD_INFO:
1380 return snd_ctl_card_info(card, ctl, cmd, argp);
1381 case SNDRV_CTL_IOCTL_ELEM_LIST:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001382 return snd_ctl_elem_list(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 case SNDRV_CTL_IOCTL_ELEM_INFO:
1384 return snd_ctl_elem_info_user(ctl, argp);
1385 case SNDRV_CTL_IOCTL_ELEM_READ:
Jaroslav Kysela42750b02006-06-01 18:34:01 +02001386 return snd_ctl_elem_read_user(card, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 case SNDRV_CTL_IOCTL_ELEM_WRITE:
1388 return snd_ctl_elem_write_user(ctl, argp);
1389 case SNDRV_CTL_IOCTL_ELEM_LOCK:
1390 return snd_ctl_elem_lock(ctl, argp);
1391 case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
1392 return snd_ctl_elem_unlock(ctl, argp);
1393 case SNDRV_CTL_IOCTL_ELEM_ADD:
1394 return snd_ctl_elem_add_user(ctl, argp, 0);
1395 case SNDRV_CTL_IOCTL_ELEM_REPLACE:
1396 return snd_ctl_elem_add_user(ctl, argp, 1);
1397 case SNDRV_CTL_IOCTL_ELEM_REMOVE:
1398 return snd_ctl_elem_remove(ctl, argp);
1399 case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
1400 return snd_ctl_subscribe_events(ctl, ip);
Jaroslav Kysela8aa9b582006-07-05 17:34:51 +02001401 case SNDRV_CTL_IOCTL_TLV_READ:
1402 return snd_ctl_tlv_ioctl(ctl, argp, 0);
1403 case SNDRV_CTL_IOCTL_TLV_WRITE:
1404 return snd_ctl_tlv_ioctl(ctl, argp, 1);
1405 case SNDRV_CTL_IOCTL_TLV_COMMAND:
1406 return snd_ctl_tlv_ioctl(ctl, argp, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 case SNDRV_CTL_IOCTL_POWER:
Takashi Iwaia381a7a2005-11-17 15:55:49 +01001408 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 case SNDRV_CTL_IOCTL_POWER_STATE:
1410#ifdef CONFIG_PM
1411 return put_user(card->power_state, ip) ? -EFAULT : 0;
1412#else
1413 return put_user(SNDRV_CTL_POWER_D0, ip) ? -EFAULT : 0;
1414#endif
1415 }
1416 down_read(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001417 list_for_each_entry(p, &snd_control_ioctls, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 err = p->fioctl(card, ctl, cmd, arg);
1419 if (err != -ENOIOCTLCMD) {
1420 up_read(&snd_ioctl_rwsem);
1421 return err;
1422 }
1423 }
1424 up_read(&snd_ioctl_rwsem);
Takashi Iwaibb009452014-02-04 18:18:16 +01001425 dev_dbg(card->dev, "unknown ioctl = 0x%x\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -ENOTTY;
1427}
1428
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001429static ssize_t snd_ctl_read(struct file *file, char __user *buffer,
1430 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001432 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int err = 0;
1434 ssize_t result = 0;
1435
1436 ctl = file->private_data;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001437 if (snd_BUG_ON(!ctl || !ctl->card))
1438 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!ctl->subscribed)
1440 return -EBADFD;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001441 if (count < sizeof(struct snd_ctl_event))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
1443 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001444 while (count >= sizeof(struct snd_ctl_event)) {
1445 struct snd_ctl_event ev;
1446 struct snd_kctl_event *kev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 while (list_empty(&ctl->events)) {
1448 wait_queue_t wait;
1449 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1450 err = -EAGAIN;
1451 goto __end_lock;
1452 }
1453 init_waitqueue_entry(&wait, current);
1454 add_wait_queue(&ctl->change_sleep, &wait);
1455 set_current_state(TASK_INTERRUPTIBLE);
1456 spin_unlock_irq(&ctl->read_lock);
1457 schedule();
1458 remove_wait_queue(&ctl->change_sleep, &wait);
Takashi Iwai0914f792012-10-16 16:43:39 +02001459 if (ctl->card->shutdown)
1460 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (signal_pending(current))
Adrian Bunk0e5d7202006-11-07 13:42:54 +01001462 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 spin_lock_irq(&ctl->read_lock);
1464 }
1465 kev = snd_kctl_event(ctl->events.next);
1466 ev.type = SNDRV_CTL_EVENT_ELEM;
1467 ev.data.elem.mask = kev->mask;
1468 ev.data.elem.id = kev->id;
1469 list_del(&kev->list);
1470 spin_unlock_irq(&ctl->read_lock);
1471 kfree(kev);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001472 if (copy_to_user(buffer, &ev, sizeof(struct snd_ctl_event))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 err = -EFAULT;
1474 goto __end;
1475 }
1476 spin_lock_irq(&ctl->read_lock);
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001477 buffer += sizeof(struct snd_ctl_event);
1478 count -= sizeof(struct snd_ctl_event);
1479 result += sizeof(struct snd_ctl_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 __end_lock:
1482 spin_unlock_irq(&ctl->read_lock);
1483 __end:
1484 return result > 0 ? result : err;
1485}
1486
1487static unsigned int snd_ctl_poll(struct file *file, poll_table * wait)
1488{
1489 unsigned int mask;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001490 struct snd_ctl_file *ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 ctl = file->private_data;
1493 if (!ctl->subscribed)
1494 return 0;
1495 poll_wait(file, &ctl->change_sleep, wait);
1496
1497 mask = 0;
1498 if (!list_empty(&ctl->events))
1499 mask |= POLLIN | POLLRDNORM;
1500
1501 return mask;
1502}
1503
1504/*
1505 * register the device-specific control-ioctls.
1506 * called from each device manager like pcm.c, hwdep.c, etc.
1507 */
1508static int _snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn, struct list_head *lists)
1509{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001510 struct snd_kctl_ioctl *pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001512 pn = kzalloc(sizeof(struct snd_kctl_ioctl), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (pn == NULL)
1514 return -ENOMEM;
1515 pn->fioctl = fcn;
1516 down_write(&snd_ioctl_rwsem);
1517 list_add_tail(&pn->list, lists);
1518 up_write(&snd_ioctl_rwsem);
1519 return 0;
1520}
1521
1522int snd_ctl_register_ioctl(snd_kctl_ioctl_func_t fcn)
1523{
1524 return _snd_ctl_register_ioctl(fcn, &snd_control_ioctls);
1525}
1526
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001527EXPORT_SYMBOL(snd_ctl_register_ioctl);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529#ifdef CONFIG_COMPAT
1530int snd_ctl_register_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1531{
1532 return _snd_ctl_register_ioctl(fcn, &snd_control_compat_ioctls);
1533}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001534
1535EXPORT_SYMBOL(snd_ctl_register_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536#endif
1537
1538/*
1539 * de-register the device-specific control-ioctls.
1540 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001541static int _snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn,
1542 struct list_head *lists)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001544 struct snd_kctl_ioctl *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001546 if (snd_BUG_ON(!fcn))
1547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 down_write(&snd_ioctl_rwsem);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001549 list_for_each_entry(p, lists, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (p->fioctl == fcn) {
1551 list_del(&p->list);
1552 up_write(&snd_ioctl_rwsem);
1553 kfree(p);
1554 return 0;
1555 }
1556 }
1557 up_write(&snd_ioctl_rwsem);
1558 snd_BUG();
1559 return -EINVAL;
1560}
1561
1562int snd_ctl_unregister_ioctl(snd_kctl_ioctl_func_t fcn)
1563{
1564 return _snd_ctl_unregister_ioctl(fcn, &snd_control_ioctls);
1565}
1566
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001567EXPORT_SYMBOL(snd_ctl_unregister_ioctl);
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#ifdef CONFIG_COMPAT
1570int snd_ctl_unregister_ioctl_compat(snd_kctl_ioctl_func_t fcn)
1571{
1572 return _snd_ctl_unregister_ioctl(fcn, &snd_control_compat_ioctls);
1573}
1574
Takashi Iwaic0d3fb32006-04-28 15:13:39 +02001575EXPORT_SYMBOL(snd_ctl_unregister_ioctl_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576#endif
1577
1578static int snd_ctl_fasync(int fd, struct file * file, int on)
1579{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001580 struct snd_ctl_file *ctl;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 ctl = file->private_data;
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001583 return fasync_helper(fd, file, on, &ctl->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*
1587 * ioctl32 compat
1588 */
1589#ifdef CONFIG_COMPAT
1590#include "control_compat.c"
1591#else
1592#define snd_ctl_ioctl_compat NULL
1593#endif
1594
1595/*
1596 * INIT PART
1597 */
1598
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08001599static const struct file_operations snd_ctl_f_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 .owner = THIS_MODULE,
1602 .read = snd_ctl_read,
1603 .open = snd_ctl_open,
1604 .release = snd_ctl_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02001605 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 .poll = snd_ctl_poll,
1607 .unlocked_ioctl = snd_ctl_ioctl,
1608 .compat_ioctl = snd_ctl_ioctl_compat,
1609 .fasync = snd_ctl_fasync,
1610};
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/*
1613 * registration of the control device
1614 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001615static int snd_ctl_dev_register(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001617 struct snd_card *card = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 int err, cardnum;
1619 char name[16];
1620
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001621 if (snd_BUG_ON(!card))
1622 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001624 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1625 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 sprintf(name, "controlC%i", cardnum);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001627 if ((err = snd_register_device(SNDRV_DEVICE_TYPE_CONTROL, card, -1,
1628 &snd_ctl_f_ops, card, name)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return err;
1630 return 0;
1631}
1632
1633/*
1634 * disconnection of the control device
1635 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001636static int snd_ctl_dev_disconnect(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001638 struct snd_card *card = device->device_data;
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001639 struct snd_ctl_file *ctl;
Takashi Iwaic4614822006-06-23 14:38:23 +02001640 int err, cardnum;
1641
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001642 if (snd_BUG_ON(!card))
1643 return -ENXIO;
Takashi Iwaic4614822006-06-23 14:38:23 +02001644 cardnum = card->number;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001645 if (snd_BUG_ON(cardnum < 0 || cardnum >= SNDRV_CARDS))
1646 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Takashi Iwaid8009882008-09-07 12:51:13 +02001648 read_lock(&card->ctl_files_rwlock);
Johannes Berg9244b2c2006-10-05 16:02:22 +02001649 list_for_each_entry(ctl, &card->ctl_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 wake_up(&ctl->change_sleep);
1651 kill_fasync(&ctl->fasync, SIGIO, POLL_ERR);
1652 }
Takashi Iwaid8009882008-09-07 12:51:13 +02001653 read_unlock(&card->ctl_files_rwlock);
Takashi Iwaic4614822006-06-23 14:38:23 +02001654
1655 if ((err = snd_unregister_device(SNDRV_DEVICE_TYPE_CONTROL,
1656 card, -1)) < 0)
1657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return 0;
1659}
1660
1661/*
1662 * free all controls
1663 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001664static int snd_ctl_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001666 struct snd_card *card = device->device_data;
1667 struct snd_kcontrol *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 down_write(&card->controls_rwsem);
1670 while (!list_empty(&card->controls)) {
1671 control = snd_kcontrol(card->controls.next);
1672 snd_ctl_remove(card, control);
1673 }
1674 up_write(&card->controls_rwsem);
1675 return 0;
1676}
1677
1678/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * create control core:
1680 * called from init.c
1681 */
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001682int snd_ctl_create(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwai82e9bae2005-11-17 13:53:23 +01001684 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 .dev_free = snd_ctl_dev_free,
1686 .dev_register = snd_ctl_dev_register,
1687 .dev_disconnect = snd_ctl_dev_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 };
1689
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001690 if (snd_BUG_ON(!card))
1691 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return snd_device_new(card, SNDRV_DEV_CONTROL, card, &ops);
1693}
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001694
1695/*
Clemens Ladisch96007322011-01-10 16:25:44 +01001696 * Frequently used control callbacks/helpers
Takashi Iwaib9ed4f22007-07-23 15:41:34 +02001697 */
1698int snd_ctl_boolean_mono_info(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_info *uinfo)
1700{
1701 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1702 uinfo->count = 1;
1703 uinfo->value.integer.min = 0;
1704 uinfo->value.integer.max = 1;
1705 return 0;
1706}
1707
1708EXPORT_SYMBOL(snd_ctl_boolean_mono_info);
1709
1710int snd_ctl_boolean_stereo_info(struct snd_kcontrol *kcontrol,
1711 struct snd_ctl_elem_info *uinfo)
1712{
1713 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1714 uinfo->count = 2;
1715 uinfo->value.integer.min = 0;
1716 uinfo->value.integer.max = 1;
1717 return 0;
1718}
1719
1720EXPORT_SYMBOL(snd_ctl_boolean_stereo_info);
Clemens Ladisch96007322011-01-10 16:25:44 +01001721
1722/**
1723 * snd_ctl_enum_info - fills the info structure for an enumerated control
1724 * @info: the structure to be filled
1725 * @channels: the number of the control's channels; often one
1726 * @items: the number of control values; also the size of @names
1727 * @names: an array containing the names of all control values
1728 *
1729 * Sets all required fields in @info to their appropriate values.
1730 * If the control's accessibility is not the default (readable and writable),
1731 * the caller has to fill @info->access.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +01001732 *
1733 * Return: Zero.
Clemens Ladisch96007322011-01-10 16:25:44 +01001734 */
1735int snd_ctl_enum_info(struct snd_ctl_elem_info *info, unsigned int channels,
1736 unsigned int items, const char *const names[])
1737{
1738 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1739 info->count = channels;
1740 info->value.enumerated.items = items;
1741 if (info->value.enumerated.item >= items)
1742 info->value.enumerated.item = items - 1;
1743 strlcpy(info->value.enumerated.name,
1744 names[info->value.enumerated.item],
1745 sizeof(info->value.enumerated.name));
1746 return 0;
1747}
1748EXPORT_SYMBOL(snd_ctl_enum_info);